473,324 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Double pointers to integers

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
2 1521


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
Thanks for posting re the double pointers. Has been of great
assistance to me.

Regards, Geoffrey Warnock.

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
53
by: Zhiqiang Ye | last post by:
Hi, All I am reading FAQ of this group. I have a question about this: http://www.eskimo.com/~scs/C-faq/q7.31.html It says: " p = malloc(m * n); memset(p, 0, m * n); The zero fill is...
62
by: onkar | last post by:
Why use pointers at all??
96
by: subramanian | last post by:
I have been thinking that all pointers(to any obejct) have the same size. The size of a pointer is the size of an int. This is beause a memory location is addressed by an int. Is that right/wrong?
5
by: Kiran | last post by:
Hi all, another newbie question from me, but here goes. Ok, I have a double array defined as follows: int cluster; now, i know that if you say for example cluster, this will give me the...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
9
by: Sarath | last post by:
Why the following happening? UINT m_uArrXpos; UINT **p = m_uArrXposSchema; // Failed to compile UINT **p = (UINT**)m_uArrXposSchema; // Success compilation
22
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never...
160
by: raphfrk | last post by:
Is this valid? int a; void *b; b = (void *)a; // b points to a b += 5*sizeof(*a); // b points to a a = 100;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.