Thread: NULL terminated arrays of struct

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    2

    NULL terminated arrays of struct

    Curious to garner some opinions on how "hacky" the following is.

    Code:
    [ ... ]
    struct MyStruct x = {
       { 0, "stuff" },
       { 1, "more" },
       NULL
    } ;
    
    int
    main( int argc, char **argv )
    {
       void **ptr ;
       struct MyStruct *x_ptr ;
    
       for( x_ptr = x ; *(ptr=x_ptr) != NULL ; x_ptr++ )
          printf( "%s\n", x_ptr->msg ) ;
    
       return EXIT_SUCCESS ;
    } ;

  2. #2
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    I think you might run into problems with that (but maybe not on your particular computer/compiler setup necessarily). I'm not certain, I've only skimmed it, but I think it's possible that the "0" first element of a struct could, in some cases, be interpreted the same as a NULL in memory. You have no idea how
    Code:
    { 0, "stuff" }
    is laid out in memory and so you could end up terminating the loop early because it thinks it's hit a NULL pointer when in fact it's just hit a zero.

    Question: Why not just use a "0" index in a fake struct entry to be your EOR indicator, and number from 1 instead, e.g.:

    Code:
    {1, "stuff"},
    {2, "more},
    {0, ""};

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    2
    Thanks for pointing that out. Ironically, the alternative suffers from (essentially) the same problem in that it defines the last entry to be a "null" (and thus assumes that no valid value will conflict). Of course, at least it can be explicit and not subject to platform/compiler variance so may be (generally) a better solution.

    My dilemma is that I'm correlating external library variables (the integers) to strings and I don't actually have explicit knowledge of the "unused" values available and so I'm just happy to terminate my static list.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It might be better off to say
    Code:
    { 0, NULL }
    An integer of 0 is likely to be valid, but a char pointer of NULL is pretty definitively invalid (you can't print it in any meaningful way).

    "" (an empty string) would also work, if you know that all valid entries must be non-empty strings.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

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. Is argv[] terminated by a NULL string?
    By dwks in forum C Programming
    Replies: 9
    Last Post: 07-24-2005, 10:24 AM
  4. null terminated
    By TeQno in forum C Programming
    Replies: 5
    Last Post: 06-06-2003, 06:10 PM
  5. Null Terminated Arrays
    By sean in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 06-17-2002, 11:39 AM

Tags for this Thread