Double pointers to integers

W

warnockg90

Hi. I am trying to write a program that utilises pointers to
dynamically allocate memory. Initially I used arrays to run the
program but this is not so good when you have user inputted values and
have to leave an excess of space when creating the arrays. I am not
having any difficulties using a single pointer to structures, but need
to create a double pointer to standard integers in memory and am not
sure how to create them or then to access the individual elements of
ints in memory.

I have a book on C but its explanations of this have been slightly
limited.

Any advice or useful links would be greatly appreciated.

Regards.
 
E

Eric Sosman

warnockg90 wrote On 11/10/05 17:13,:
Hi. I am trying to write a program that utilises pointers to
dynamically allocate memory. Initially I used arrays to run the
program but this is not so good when you have user inputted values and
have to leave an excess of space when creating the arrays. I am not
having any difficulties using a single pointer to structures, but need
to create a double pointer to standard integers in memory and am not
sure how to create them or then to access the individual elements of
ints in memory.

I have a book on C but its explanations of this have been slightly
limited.

Any advice or useful links would be greatly appreciated.

Perhaps an example would help. Here's a sketch of
how to build a "triangular" array: an array whose elements
are themselves arrays, the first containing one int, the
next two, and so on until the last contains N ints.

int **array;
array = malloc(N * sizeof *array);
if (array == NULL) die();
for (i = 0; i < N; ++i) {
array = malloc((i + 1) * sizeof *array);
if (array == NULL) die();
}

Pictorially (that's a c.l.c. code word meaning "terrible
ASCII art follows"), you get something like

array ------+
|
V
+-----------+ +-------------+
| array[0] ---> | array[0][0] |
+-----------+ +-------------+
| array[1] ---+
+-----------+ | +-------------+-------------+
: : +--> | array[1][0] | array[1][1] |
+-------------+-------------+

Variations: The "rows" could all be the same length,
they could be of different lengths but not in a neat
pattern (a "ragged array"), some of the array pointers
could be NULL (an array with some "rows" missing), and so
on. Try to maintain the picture: the variable `array'
points to an array filled with pointers, each of these
in turn points to an array filled with ints -- or structs,
or chars (an "array of strings"), or whatever.
 
W

warnockg90

Thanks for posting re the double pointers. Has been of great
assistance to me.

Regards, Geoffrey Warnock.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top