0

I'm currently working on an assignment in my Intro to C class. The assignment has us using fgets to read a single line from a file, then strcpy and strtok to take individual elements from that line and store them into different arrays. The file is a list of comic book character info, so we would store the character's first name into an array, last name in a different array, age into a different array, etc. Here is an example of one line from the file:

Romanoff , Natasha ; F Black Widow 1964 Marvel 52 Avengers 1975 green Human Auburn 170 59

I've had success storing the last and first names using strtok to stop at the punctuation separating the names. I'm running into a problem with the next bit, however, which is storing the character's sex (in this case, "F") into an array for gender. Since there is no punctuation or numbers between it and the next bit of info, I'm not sure how to proceed.

Initially, I tried reading the character from the file in the same way that I did the other items, using a space " " as the delimiter in strtok:

strcpy(gender[count], strtok(NULL, " "));

When I do this, I receive the following error:

warning: passing argument 1 of 'strcpy' makes pointer from integer without a cast [-Wint-conversion]

I'm thinking that strtok is picking up on the space immediately following the ";" and stopping there before even getting to "F" and copying that to gender[count]. But I don't know how to skip over that space so that the "F" is successfully copied.

जलजनक
  • 3,072
  • 2
  • 24
  • 30
  • 2
    The error is complaining about `gender[count]`, not about the result of `strtok` (which will always be a pointer). I'm guessing that `gender` is a `char[]`, in which case you just use regular assignment, e.g.: `gender[count] = *strtok(NULL," ");` – UnholySheep Mar 29 '22 at 22:48
  • That fixed it exactly. I still have no understanding of what pointers do, but at least I can get this assignment turned in. Thank you very much @UnholySheep! – JakeFist26 Mar 29 '22 at 22:56

0 Answers0