Strings and arrays

J

Jim

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

Jim

BTW - MAXWORDS is the maximum number of words that will be broken out
and WORDSIZE is the maximum number of characters per word.
 
F

fred.l.kleinschmidt

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.
 
J

Jim

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.

so how can I fix that?
 
B

Ben Bacarisse

Jim said:
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.

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.
 
J

Jim

Jim said:
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.

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.

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

CBFalconer

Jim said:
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/>
 
B

Ben Bacarisse

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.
 
T

Thad Smith

Jim said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top