I am writing this method (C - Language) which should create a new node for a linked list.
It crashes at the line just after the first if
(SIGSEGV signal)
I am debugging the method so probably there are more errors in the subsequent lines, but for the moment I will appreciate any observations regarding this particular line.
//Create a new tEquivalenceNode
tEquivalenceNode* createNewEquivalenceNode(tWordEquivalence e){
//create new equivalence node
tEquivalenceNode* node = NULL;
node = (tEquivalenceNode*) malloc(sizeof(tEquivalenceNode));
//allocate memory for both srcWord and dstWord
tWordInfo* destiny = NULL;
tWordInfo* source = NULL;
destiny = (tWordInfo*) malloc(sizeof(tWordInfo));
source = (tWordInfo*) malloc(sizeof(tWordInfo));
if((node != NULL)&&(destiny != NULL) && (source != NULL)){
node->elem->dstWord = destiny; //Crashes at this line
node->elem->srcWord = source;
//copy information to destiny word in the new Node
node->elem->dstWord->languageID = e.dstWord->languageID;
node->elem->dstWord->wordID = e.dstWord->wordID;
//allocate memory for word and copy the string to the node
node->elem->dstWord->word = (char*) malloc((strlen(e.dstWord->word) + 1)*sizeof(char));
if(node->elem->dstWord->word != NULL){
strcpy(node->elem->dstWord->word, e.dstWord->word);
}
//repeat the process for source word
node->elem->srcWord->languageID = e.srcWord->languageID;
node->elem->srcWord->wordID = e.srcWord->wordID;
//allocate memory for word and copy the string to the node
node->elem->srcWord->word = (char*) malloc((strlen(e.srcWord->word) + 1)*sizeof(char));
if(node->elem->srcWord->word != NULL){
strcpy(node->elem->srcWord->word, e.srcWord->word);
}
node->next = NULL;
}
return node;
}
This is the definition of the tWordInfo data type:
/* Dictionary Word */
typedef struct {
int languageID;
int wordID;
char* word;
} tWordInfo;
tEquivalenceNode:
/* Equivalences list element */
typedef struct tEquivalenceNode tEquivalenceNode;
struct tEquivalenceNode{
/* EX 1.1 */
tWordEquivalence* elem;
tEquivalenceNode* next;
};
and tWordEquivalence:
typedef struct {
/* Word in the source language */
tWordInfo* srcWord;
/* Equivalent word in destination language */
tWordInfo* dstWord;
} tWordEquivalence;