Strings and arrays

Discussion in 'C Programming' started by Jim, Mar 28, 2008.

  1. Jim

    Jim Guest

    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);
    Jim, Mar 28, 2008
    #1
    1. Advertising

  2. Jim

    Jim Guest

    BTW - MAXWORDS is the maximum number of words that will be broken out
    and WORDSIZE is the maximum number of characters per word.
    Jim, Mar 28, 2008
    #2
    1. Advertising

  3. Jim

    Guest

    On Mar 28, 10:02 am, Jim <> 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
    , Mar 28, 2008
    #3
  4. Jim

    Jim Guest

    On Mar 28, 1:09 pm, wrote:
    > On Mar 28, 10:02 am, Jim <> 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?
    Jim, Mar 28, 2008
    #4
  5. Jim <> writes:

    > On Mar 28, 1:09 pm, wrote:
    >> On Mar 28, 10:02 am, 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);
    >> > 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.
    Ben Bacarisse, Mar 28, 2008
    #5
  6. Jim

    Jim Guest

    On Mar 28, 1:50 pm, Ben Bacarisse <> wrote:
    > Jim <> writes:
    > > On Mar 28, 1:09 pm, wrote:
    > >> On Mar 28, 10:02 am, 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);
    > >> > 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.
    Jim, Mar 28, 2008
    #6
  7. Jim

    CBFalconer Guest

    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
    CBFalconer, Mar 28, 2008
    #7
  8. 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.
    Ben Bacarisse, Mar 28, 2008
    #8
  9. Jim

    Thad Smith Guest

    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
    Thad Smith, Mar 29, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bill Reyn
    Replies:
    3
    Views:
    2,254
    Bob Hairgrove
    Jun 22, 2004
  2. kelvSYC

    Arrays and Pointers to Arrays

    kelvSYC, Sep 26, 2003, in forum: C Programming
    Replies:
    2
    Views:
    385
  3. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    757
    Malcolm
    Jun 24, 2006
  4. Philipp
    Replies:
    21
    Views:
    1,127
    Philipp
    Jan 20, 2009
  5. Pieter Hugo
    Replies:
    13
    Views:
    210
    Robert Klemme
    Nov 26, 2009
Loading...

Share This Page