Thread: Array of pointers

  1. #1
    Registered User falconetti's Avatar
    Join Date
    Nov 2001

    Question Array of pointers

    Hello all.

    I have a question regarding arrays of pointers.

    Letīs say I want to create an array of pointers, each pointer pointing to an array of integers. I have 5 arrays of integers, that means that I will need an array of 5 pointers to integers. Well, in my book, every time something like this is to be done the author codes an array of 6 pointers, instead of 5. I mean 1 extra pointer, that he initializes to NULL.

    I just donīt understand why I need an extra pointer, initialized to NULL.

    Any help will be appreciated.


  2. #2
    Join Date
    Aug 2001
    Groningen (NL)
    What does the author do with it? Can you give an example? Perhaps he uses this to determine if he has reached the end or something like that.

  3. #3
    Registered User falconetti's Avatar
    Join Date
    Nov 2001

    Post An example

    Ok, here comes an example from the book:

    table = (int **)calloc (rowNum + 1, sizeof (int *));
    table[0] = (int *)calloc (4, sizeof (int));
    table[1] = (int *)calloc (2, sizeof (int));
    table[2] = (int *)calloc (7, sizeof (int));
    table[3] = (int *)calloc (1, sizeof (int));
    table[4] = (int *)calloc (3, sizeof (int));
    table[5] = NULL;
    This code is meant to create a ragged array of 5 rows (rowNum = 5)

    Why the need of table[5] ?

  4. #4
    Registered User
    Join Date
    Sep 2001
    It's a NULL terminated array. Basically, there's three ways to handle having a pointer to a string of elements. You can either...

    A) Hard code it to handle the right number of elements (this only works if you know how many elements it will have at compile time)
    B) Keep a variable that keeps track of how many elements there are. This is basically the same as A), except it can handle run-time.
    C) Keep a terminator value at the end of the array.

    Your text uses C). There are different advantages to B) and C). I'm guessing that he's using this method so that he can use the same methonds on those ragged arrays that he defines as ones that are built at run-time.
    Callou collei we'll code the way
    Of prime numbers and pings!

  5. #5
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002

    Re: An example

    Originally posted by falconetti
    Why the need of table[5] ?
    It isn't needed but, as Shiro suggested, it could be used to know when you've reached the end:

    for ( i=0; table[i]; i++ )
      /* whatever */
    This loop executes as long as table[i] is not NULL. In the code example you provided, the for statement would work its way through table and stop when it reached the sixth element (table[5]).
    Jason Deckard

  6. #6
    Registered User falconetti's Avatar
    Join Date
    Nov 2001

    Smile Thanks

    Now the whole thing starts to make sense.

    Thank you all !!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  2. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. array of pointers to struct array
    By eth0 in forum C++ Programming
    Replies: 1
    Last Post: 01-08-2004, 06:43 PM
  5. array of pointers to structs
    By stumon in forum C Programming
    Replies: 7
    Last Post: 03-24-2003, 07:13 AM