#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    3

    Pointer to array and STRTOK() function question


    I'm writing a program that recieves a text and creates a list of all the different words in alphabetical order, and for each word-- the number of the first line it appeared in.

    i.e- I create a list where all words are diffrent. I use strtok() to catch the words and one of the functions is:

    int create_list(char text[][40],char words[][40],char new_words[][20],int line_number[],int i)
    {
    static int n, k;
    int j;
    char *ptr;
    const char PUNCTS[] = " ,;?:.!\"/+-}{)([]*&%#$@\\=-|";
    if(i==0)
    {
    k=0;
    ptr=strtok(text[i],PUNCTS);
    &words[k]=ptr; <---------(HERE IS THE PROBLEM)----------> INVALID VALUE IN ASSIGNMENT.
    n=0;
    *new_words[n]=*words[0];
    line_number[n]=0;
    }

    while(ptr!=NULL)
    {
    ......
    **Here i deal with all the rest lines**
    ......
    ......
    }

    The compiler shows me that "&words[k]=ptr" is an INVALID VALUE IN ASSIGNMENT.

    I've read about arrays and pointers, and i'm familiar with the different syntaxes, still i don't get what he wants- can anyone help ?
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    words[k]=ptr; <---------(HERE IS THE PROBLEM)----------> INVALID VALUE IN ASSIGNMENT.
    You want to to store a copy of that token into words[k]. So copy it already! strcpy().
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,113
    Rep Power
    1803
    The assignment attempts to change teh address of an array - which is obviously nonsensical. What you presumably intended was to copy the data that ptr points to words[k].

    Code:
    strcpy( words[k], ptr ) ;
    You probably ought to check ptr for NULL before attempting to use it however.

    Similarly this:
    Code:
    *new_words[n]=*words[0];
    seems unlikly to do what you intended. It is equivalent to:
    Code:
    new_words[n][0] = words[0][0];
    It copies just one character.

    Arrays in C are not first-class data types, you cannot assign them, and you cannot pass them by-copy as parameters.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    3
    Thanks a lot. Got it.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    May 2004
    Posts
    3,417
    Rep Power
    887
    Please learn to use code tags to maintain proper code formatting. You can go back and edit the original message, remove the code, add the code tags and then paste your original source, from your editor, between the tags. That will preserve indentation and make your code much easier to read.
    I no longer wish to be associated with this site.

IMN logo majestic logo threadwatch logo seochat tools logo