Double Pointer

What is a Double Pointer? What are its specific uses.

Questions by swati.kariya

Showing Answers 1 - 12 of 12 Answers

rashmi nayak

  • Sep 10th, 2011
 

int i=10;
int *p;
p=&i;
int **q;
q=&p;
this means a pointer pointing to a pointer which in turn points to an integer.....this is called double pointer....

  Was this answer useful?  Yes

garima

  • Oct 6th, 2011
 

double pointers are useful whenever you have defined user defined pointers in your main function. And you want to change their values from outside the main like in functions other than main. There the use of pointers is must.

  Was this answer useful?  Yes

By double pointers I assume you're talking about something like **ptr, as opposed to a simple pointer to a double object.

Multiple levels of indirection show up in the following areas:

1. You want a function to modify a pointer value. To do this, you must pass a pointer to the pointer:

void openAndInit(FILE **stream)
{
*stream = fopen(some_file, "w");
fprintf(*stream, "some initial data");
}

int main(void)
{
FILE *dat;
...
openAndInit(&dat);
...
}

2. We want to dynamically allocate a multi-dimensional array:

int **x;
x = malloc(sizeof *x * ROWS);
if (x)
{
int i;
for (i = 0; i < ROWS; i++)
{
x[i] = malloc(sizeof *x[i] * COLS);
}
}

Those are two uses off the top of my head; they aren't the only ones.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions