comp.lang.c
[Top] [All Lists]

Re: Strings and arrays

Subject: Re: Strings and arrays
From: Jim
Date: Fri, 28 Mar 2008 10:13:29 -0700 PDT
Newsgroups: comp.lang.c

On Mar 28, 1:09 pm, fred.l.kleinschm...@xxxxxxxxxx wrote:
> On Mar 28, 10:02 am, Jim <jim.moneyt...@xxxxxxxxx> wrote:
>
>
>
> > Hi There.
>
> > I have to create a function that will take a sentence and breakout the
> > words and save them into an array.  For some reason, I can't get it
> > working.  I keep getting a segmentation fault being caused by
> > strcpy().  Can someone please tell me what I'm doing wrong:
>
> >      88     void displayPigLatin(char *str) {
> >      89     void encode(char *sPtr);
> >      90
> >      91     char
> >      92         *arrStr[MAXWORDS][WORDSIZE],
> >      93         *tokenPtr,
> >      94         *tmpStr;
> >      95
> >      96     int cnt;
> >      97
> >      98     strcpy(arrStr[0][0], str);
> >      99     tokenPtr = strtok(arrStr[0][0], " ");
> >     100     printf("%s\n", tokenPtr);
>
> arrStr[0][0] has not yet been set to point to any storage location.
> When you copy str to it, you are copyihng to some random place,
> corrupting memory.
> --
> Fred Kleinschmidt

so how can I fix that?

<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy