S
sheroork
Can anyone help me in getting to understand pointer to pointer with
examples?
Appritiate in advance.
Sagar
examples?
Appritiate in advance.
Sagar
Can anyone help me in getting to understand pointer to pointer with
examples?
sheroork said:Can anyone help me in getting to understand pointer to pointer with
examples?
Richard Heathfield said:sheroork said:
Examples are tricky, in that any realistic working example is likely to be
way too big and complicated to make it a useful example. If you're not
afraid of big examples, though, let me know and I'll see what I can dig up
from my code base.
Using a pointer to pointer to return a pointer value from a
function may be a simple way to come up with a short example.
Richard Heathfield said:Maybe August had the right idea - a dynamic array of dynamic arrays.
August said:int **p;
p
+---+
| ? |
+---+
p = malloc(sizeof *p);
p *p
+---+ +---+
| ----->| ? |
+---+ +---+
*p = malloc(sizeof **p);
p *p **p
+---+ +---+ +---+
| ----->| ----->| ? |
+---+ +---+ +---+
**p = 7;
p *p **p
+---+ +---+ +---+
| ----->| ----->| 7 |
+---+ +---+ +---+
August
August Karlstrom wrote:
ITYM: *p=malloc(sizeof int);
Richard said:Scorpio said:
No, HDNMT. For one thing, August's code is conceptually correct no matter
what the object type to which p points. And secondly, his code - unlike
yours - will actually compile.
Sorry, I was mistaken and yes, August's code is correct.
But I actually meant: *p=malloc(sizeof(int));
Is something wrong with this?
Even the code quoted by Ben from the FAQ is as follows:
int **array1 = malloc(nrows * sizeof(int *));
for(i = 0; i < nrows; i++)
array1 = malloc(ncolumns * sizeof(int));
BTW, what does HDNMT mean? I did try to find out, but found no results.
Morris said:sheroork (in (e-mail address removed))
said:
| Can anyone help me in getting to understand pointer to pointer with
| examples?
Sagar...
The explanations posted so far look good. These two links make up
another example of how pointers to pointers might be used:
http://www.iedu.com/mrd/c/tokenize.c
http://www.iedu.com/mrd/c/tokfile.c
August said:int **p;
p
+---+
| ? |
+---+
p = malloc(sizeof *p);
p *p
+---+ +---+
| ----->| ? |
+---+ +---+
*p = malloc(sizeof **p);
p *p **p
+---+ +---+ +---+
| ----->| ----->| ? |
+---+ +---+ +---+
**p = 7;
p *p **p
+---+ +---+ +---+
| ----->| ----->| 7 |
+---+ +---+ +---+
Richard said:Scorpio said:
It will at least compile, but it's inferior to August's version,
because you're nailing the type to the call, possibly causing a
maintenance headache further down the line.
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.