Thread: strtok() and struct

  1. #1
    Registered User
    Join Date
    Apr 2020

    strtok() and struct

    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?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2019, 07:51 AM
  2. strtok() help
    By otaconBot in forum C Programming
    Replies: 11
    Last Post: 08-07-2012, 09:09 AM
  3. strtok
    By Smithy in forum C Programming
    Replies: 6
    Last Post: 04-09-2010, 07:01 PM
  4. struct holding data inside a linked list struct
    By icestorm in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 12:49 PM
  5. help with strtok
    By requiem in forum C Programming
    Replies: 5
    Last Post: 04-29-2003, 04:10 PM

Tags for this Thread