LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-02-2005, 11:10 AM   #1
peppem
LQ Newbie
 
Registered: Dec 2005
Posts: 2

Rep: Reputation: 0
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;
}
 
Old 12-02-2005, 11:17 AM   #2
peppem
LQ Newbie
 
Registered: Dec 2005
Posts: 2

Original Poster
Rep: Reputation: 0
an easy question

forget the other question.Can you tell me please what's wrong with this program?Why does it give me a segmantation fault?:[list]

void tokenize(char *sentence){
char **tokenPtr=NULL;
int i=0;
tokenPtr[0]=strtok(sentence," ");
while(tokenPtr[i]!=NULL){
i++;
tokenPtr[i]=strtok(NULL," ");
printf("%s\n",tokenPtr[i]);
}
}

int main()
{
char sentence[]="ali topu tut";
tokenize(sentence);
return 0;
}

please help!
 
Old 12-02-2005, 11:31 AM   #3
FLLinux
Member
 
Registered: Jul 2004
Location: USA
Distribution: Fedora 9, LFS 6.3, Unbuntu 8.04, Slax 6.0.7
Posts: 145

Rep: Reputation: 15
I think since you are declaring char **tokenPtr = NULL; you only have one pointer so when you try to access tokenPtr[1] there is nothing there. what you might want to do is this

void tokenize(char *sentence)
{
char *tokenPtr = NULL;
int i = 0;
tokenPtr = strtok(sentence, " ");
while(tokenPtr != NULL)
{
i++;
tokenPtr = strtok(NULL, " ");
printf("%s\n", tokenPtr);
}
}

That should do what you want it to do.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmantation fault in mindless automaton and some other games dragonslay Linux - Games 3 03-31-2005 09:34 PM
Tripwire - Segmantation falut manudath Linux - Software 1 03-27-2005 06:20 PM
Segmantation fault... solution ghaefb Programming 3 11-26-2004 03:46 AM
AOL problem I wanted to inform you all about! WorldBuilder Linux - General 1 06-20-2003 03:14 PM
what is segmantation fault (core dumped) jolly Linux - General 5 10-01-2001 09:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration