# Array of pointers

• 01-11-2002
falconetti
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.

Thanks.
• 01-11-2002
Shiro
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.
• 01-11-2002
falconetti
An example
Ok, here comes an example from the book:

Code:

```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] ?
• 01-11-2002
QuestionC
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.
• 01-11-2002
Deckard
Re: An example
Quote:

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:

Code:

```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]).
• 01-11-2002
falconetti
Thanks
Now the whole thing starts to make sense.

Thank you all !!