segmantation problem while compiling,help wanted
ı just couldn't figure out what ı'm doing wrong.When ı compile this program, it gives segmantation fault even though ı've been very careful about the memory allocation.Can you please give me a hand?
the types:
typedef struct word_node *WORDPTR;
typedef struct word_node{
char letter;
WORDPTR next;
} w_node;
typedef struct sentence_node *SENTENCEPTR;
typedef struct sentence_node{
WORDPTR data;
SENTENCEPTR next;
}s_node;
typedef struct word{
WORDPTR k_son_isr;
}WORD;
typedef struct sentence{
SENTENCEPTR c_son_isr;
}SENTENCE;
the function:
SENTENCEPTR stringToSentence(char *sentence)
{
char **tokenPtr;
int i=0,j=0,l=0;
SENTENCEPTR cPtr;
WORDPTR kPtr;
SENTENCE c;
WORD k;
tokenPtr[0]=strtok(sentence," ");
while(tokenPtr[i]!=NULL){
i++;
tokenPtr[i]=strtok(NULL," ");
}
if((cPtr=(SENTENCEPTR)malloc(sizeof(s_node)))!=NULL){
c.c_son_isr=cPtr;
for(j=0,cPtr=cPtr->next;j<=i-1;j++,cPtr=cPtr->next){
if((cPtr=(SENTENCEPTR)malloc(sizeof(s_node)))!=NULL){
if((kPtr=(WORDPTR)malloc(sizeof(w_node)))!=NULL)
{
k.k_son_isr=kPtr;
for(l=0,kPtr=kPtr->next;l<strlen(tokenPtr[j]);l++,kPtr=kPtr->next)
{
if((kPtr=(WORDPTR)malloc(sizeof(w_node)))!=NULL)
{
kPtr->letter=*(tokenPtr[j]+l);
}
}
kPtr=k.k_son_isr->next;
cPtr->data=k.k_son_isr;
}
}
}
cPtr=c.c_son_isr->next;
}
return c.c_son_isr;
}
|