Thread: null terminated string lists

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    596

    null terminated string lists

    Is it common practice, or accepted practice, to terminate a list of strings with a null string?

    For example,
    Code:
    char *list[] = {"string A", "string B", "string C", ""};
    This would be for the purpose of handling the string list without having to use a separate associated value for the list size.

    -

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There's a precedent in argv, though that approach uses a null pointer rather than an empty string, and also provides for argc anyway. Personally I think the problems with the null character to terminate strings (i.e., often seen as the weak link behind buffer overflow vulnerabilities) provides us a lesson that it tends to be better to maintain the list size rather than to terminate with a special value.
    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

  3. #3
    Banned
    Join Date
    Oct 2014
    Location
    Home
    Posts
    135
    Terminating with a certain delimiter has the potential problem of displacing the character for that sole purpose. So you don't have use for that character when you need it anymore. So if you need all your character sets laserlight is right in keeping a tally manually.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Ok, thanks.

    I think I will combine the ponters to lists and the list size in an array of structs then.

    -

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Non null-terminated strings
    By Casey G in forum C++ Programming
    Replies: 5
    Last Post: 05-10-2011, 07:47 PM
  2. Assigning a non null-terminated string
    By BattlePanic in forum C Programming
    Replies: 7
    Last Post: 05-04-2008, 10:02 PM
  3. Problem with my NULL-terminated string class
    By elfjuice in forum C++ Programming
    Replies: 3
    Last Post: 02-25-2006, 10:39 PM
  4. Is argv[] terminated by a NULL string?
    By dwks in forum C Programming
    Replies: 9
    Last Post: 07-24-2005, 10:24 AM
  5. null terminated
    By TeQno in forum C Programming
    Replies: 5
    Last Post: 06-06-2003, 06:10 PM

Tags for this Thread