Is there any chance that I can use strtok() to insert data (and as a result add new node to a struct)in a struct with many members inside that?
Is there any chance that I can use strtok() to insert data (and as a result add new node to a struct)in a struct with many members inside that?
Yes, you have a chance with at least three ways:
- Copy the token to an array of char member in the struct. However, you need to know how long the longest token can be in order to declare the array size in advance.
- Allocate memory via a pointer to char member in the struct, then copy the token to the memory allocated. However, you need to deallocate the memory when done.
- Store a copy of the pointer to the token in a pointer to char member in the struct. However, you must ensure that the array of char that held the original string that strtok operated on remains valid (e.g., it cannot be a non-static local variable that goes out of scope while you're still using the struct) and doesn't get reused for some other purpose.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)