By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,944 Members | 1,299 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,944 IT Pros & Developers. It's quick & easy.

Double pointers to integers

P: n/a
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.

Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a


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[i] = malloc((i + 1) * sizeof *array[i]);
if (array[i] == 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[i] 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.

--
Er*********@sun.com

Nov 15 '05 #2

P: n/a
Thanks for posting re the double pointers. Has been of great
assistance to me.

Regards, Geoffrey Warnock.

Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.