Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Strings and arrays

Reply
Thread Tools

Strings and arrays

 
 
Jim
Guest
Posts: n/a
 
      03-28-2008
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);
 
Reply With Quote
 
 
 
 
Jim
Guest
Posts: n/a
 
      03-28-2008
BTW - MAXWORDS is the maximum number of words that will be broken out
and WORDSIZE is the maximum number of characters per word.
 
Reply With Quote
 
 
 
 
fred.l.kleinschmidt@boeing.com
Guest
Posts: n/a
 
      03-28-2008
On Mar 28, 10:02*am, Jim <jim.moneyt...@gmail.com> 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
 
Reply With Quote
 
Jim
Guest
Posts: n/a
 
      03-28-2008
On Mar 28, 1:09 pm, fred.l.kleinschm...@boeing.com wrote:
> On Mar 28, 10:02 am, Jim <jim.moneyt...@gmail.com> 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?
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      03-28-2008
Jim <> writes:

> On Mar 28, 1:09 pm, fred.l.kleinschm...@boeing.com wrote:
>> On Mar 28, 10:02 am, Jim <jim.moneyt...@gmail.com> wrote:
>> > 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


best not to quote sigs...

> so how can I fix that?


By not doing it! Ok, glib, but how can anyone guess how you intended
this to work? You might have intended to copy each word into the
WORDSIZE char spaces in the arrStr variable. In that case the
declaration is wrong (you don't need the *).

You might have intended the arrStr to be an array of pointers, but
only a 1D array (then the * is right and the [WORDSIZE] is wrong).
The plan might have been to point to each word returned by strtok.

In both cases the initial call to strcpy seems wrong. It is a good
idea to copy the string before letting strtok loose on it, but the
copy as you have it seems out of place.

I would copy the string counting words as I go. Then I'd allocate
(with malloc) an array of pointers big enough for the job (we now
know having counted) and then I'd scan again, setting each of the
pointers to the right place in the copied string and setting the first
' ' to '\0' as I go. I would not bother with strtok at all.

--
Ben.
 
Reply With Quote
 
Jim
Guest
Posts: n/a
 
      03-28-2008
On Mar 28, 1:50 pm, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
> Jim <jim.moneyt...@gmail.com> writes:
> > On Mar 28, 1:09 pm, fred.l.kleinschm...@boeing.com wrote:
> >> On Mar 28, 10:02 am, Jim <jim.moneyt...@gmail.com> wrote:
> >> > 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

>
> best not to quote sigs...
>
> > so how can I fix that?

>
> By not doing it! Ok, glib, but how can anyone guess how you intended
> this to work? You might have intended to copy each word into the
> WORDSIZE char spaces in the arrStr variable. In that case the
> declaration is wrong (you don't need the *).
>
> You might have intended the arrStr to be an array of pointers, but
> only a 1D array (then the * is right and the [WORDSIZE] is wrong).
> The plan might have been to point to each word returned by strtok.
>
> In both cases the initial call to strcpy seems wrong. It is a good
> idea to copy the string before letting strtok loose on it, but the
> copy as you have it seems out of place.
>
> I would copy the string counting words as I go. Then I'd allocate
> (with malloc) an array of pointers big enough for the job (we now
> know having counted) and then I'd scan again, setting each of the
> pointers to the right place in the copied string and setting the first
> ' ' to '\0' as I go. I would not bother with strtok at all.
>
> --
> Ben.


I am trying to pass an array of strings into the function.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      03-28-2008
Jim wrote:
>
>
> 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:


Lots of things. Consider a more flexible structure. Take a look
at how id2id-20 works. This effectively translates arbitrary words
into other arbitrary words. Available at:

<http://cbfalconer.home.att.net/download/>

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      03-28-2008
Jim <> writes:
<snip unrelated stuff>

> I am trying to pass an array of strings into the function.


OK. Such a parameter will be a 'char **s_array' or, if you want to
make the arrayness more obvious, 'char *s_array[]'. The calling
function will declare a 'char *stings[SIZE];' object and pass that.

--
Ben.
 
Reply With Quote
 
Thad Smith
Guest
Posts: n/a
 
      03-29-2008
Jim wrote:

> 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);

....

Fred and Ben have discussed details of your code. I recommend addressing
the problem as one of specification.

You said that you have to "create a function that will take a sentence and
breakout the words and save them into an array." Let's start there. Let's
specify three things:
1. Exactly what the input to the function is.
2. What the function will do.
3. What the result will be.

Input:
You have already said that it will "take a sentence". One reasonable
implementation is that the sentence is contained in a C string. Is that
OK? Since it is a sentence, I assume that it may be terminated with
punctuation. Since you are looking for word, they will probably get
discarded, but you should say so.

Processing:
You said that it will "break out [two separate words in this use] the words
and save them in an array." You need to define how words are delimited.
Usually this will be a space, but could be punctuation. How do you want to
treat hyphens separating letters? It would probably be easiest to assume
they separate words. How about apostrophe? The best assumption is that
they are contained within a word -- in other words treat them similar to
letters.

You want to save the words in an array. How will they be stored in an
array? Will you have an array for a fixed number of words? If so, what
happens if more words are found? What will you do with duplicate words --
enter them for each instance or only once? Will you retain the original
capitalization? Will the words be stored in a two dimensional array or be
stored in a separately allocated array with a pointer in the array of words?

Output:
Finally, what will the function do when it has placed the words in an
array? Will it pass it back to the caller? If so, will the address of the
array be passed from the caller to the function, or will the function
allocate the array dynamically and return the address of the array to the
caller? Will it instead be placed in an array accessible to both caller
and the function (usually not a good idea)? How will the end of the word
list be indicated?

These are the details that I would write in the header to such a function.
A user should be able to read that header and know what the results will
be without looking at the code.

Once that is done, writing the code becomes much easier. You may specify
one thing and then change your mind during development. That's OK -- just
remember to change the description to match.

--
Thad
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between << and += for Strings and Arrays. Bug? Pieter Hugo Ruby 13 11-26-2009 10:16 AM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
char arrays and integer arrays... why the difference? Bill Reyn C++ 3 06-22-2004 12:01 PM
Arrays and Pointers to Arrays kelvSYC C Programming 2 09-26-2003 06:52 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57