Thread: Dynamic array of structs.. sentinal value?

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    269

    Dynamic array of structs.. sentinal value?

    Hi, I basically want to dynamically allocate an array, and then pass this pointer on, but I don't want to pass any specific size along, I just want to use a sentinal to mark the end of the array.

    I thought calloc would do it, but it doesn't...

    Code:
    int main()
    {
      struct test
      {
        int number;
        int list[10];
      };
      typedef struct test test;
    
      test * list = (test*)calloc(10, sizeof(test));
      
      int i = 0;
      while(&list[i] != NULL)
        {
          printf("list index %d is not null.\n",i);	     
          i++;
        }
      return 0;
    }
    It just basically goes on forever... i should stop at the first element.....


    Any advice?

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your struct consists of 11 integers, all of which are set to zero by calloc.

    But your loop tests the ADDRESS of the elements of list, none of which will be NULL.

    With the given struct, your sentinel must be some value in one of those ints. Perhaps number could be 0 (if that's an invalid value), or -1.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    269
    So, I guess I'd first have to initalize each element in the array to the sentinel value (say, where number = -1 or something), and then my function will set the number of elements in the array accordingly

    then in my while loop, i'd check for number == -1, and that's when I stop...

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    or you could use an array of pointers and use NULL as the sentinel value. especially with structs, this is the way I would choose.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I got a little confused because there's a "list" member in the struct and your array of structs is also called "list". Sorry.

    Anyway, as Elkvis said, you could allocate an array of pointers to your struct, all initialized to NULL (which technically you should do manually and not rely on calloc). Then you'd allocate a struct to each one that you use.

    The other way is to allocate all the structs as you are doing and then, within each struct, set "number" to 0, -1, INT_MIN, INT_MAX (whichever is "out-of-band" for your data), and use that as your sentinel value.

    I can't tell what's best from what you've shared. I'd have to see (or have described) the whole program and it's ultimate purpose in order to propose something "better".
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-20-2011, 09:43 PM
  2. Replies: 3
    Last Post: 03-31-2009, 12:34 PM
  3. Dynamic Array of Structs help
    By fairguynova in forum C++ Programming
    Replies: 5
    Last Post: 02-04-2009, 11:20 PM
  4. Dynamic Array of Structs Help
    By Spawn in forum C++ Programming
    Replies: 3
    Last Post: 04-29-2006, 05:45 PM
  5. dynamic arrays of structs...
    By matheo917 in forum C++ Programming
    Replies: 8
    Last Post: 12-14-2002, 06:57 AM