Thao Nguyen
Thao Nguyen

Reputation: 901

Tokenizing a string in C

I'm trying to store a string with a first and last name from a string into a struct but I'm getting (warning: passing argument 1 of strcpy makes pointer from integer without a cast), and I'm not sure on where to put strcpy tried putting it in the while loop got the error which makes sense. But not sure on where to place strcpy EDITED

 struct trip

    {
     char first_name;
     char last_name;

    }
    int main(void)
    {
     struct trip travel[12];
    }

    char input_name(struct trip travel[MAXTRIP], int index)
    {
      int name_read, length; 
      int name_bytes = 100;
      char *name, *word;                 

      getchar();
      printf("Please enter name:\n");

      name = (char *)malloc(name_bytes + 1);
      name_read = getline (&name, &name_bytes, stdin);

      word = strtok(name, ",");
      while (word != NULL)
        {
          strcpy(travel[index].first_name, word);
          word = strtok(NULL, ",");

        }


    }

Upvotes: 0

Views: 404

Answers (1)

pmg
pmg

Reputation: 108986

Ignoring the (MANY) errors in your code, you are putting the strcpy() in the right place.

However, you are not calling it with the correct arguments: strcpy() needs 2 arguments.
Basically, both are of type char*; you are passing a char and a char* and that is why the compiler complains (for the compiler the char behaves like an int so it says "strcpy makes pointer from integer").

You need to review your data structure and pass the right char*s to strcpy().

Upvotes: 4

Related Questions