Hi guys,
can anybody explain to me with a example why we people emphasize to pass double pointer in a function instead a single pointer when we want to fill some variable inside that function.
or what are thge advantage of double pointers in c over single pointer?

A double pointer has two basic meanings. One is of a pointer to a pointer, where changing the value of double pointer will result in the original pointer being changed. Another is that of a two-dimentional array, such as a matrix, or a list of char* (e.g. in main when you use argv).

I've probably over-generalized, but hopefully that gets the idea across ;)

thankx Infarction for ur reply..
but whatever you have said it is something like bookish knowledge .. i think i ll get more clear answer from people who use double pointer in their coding style..
so plz people clarify my doubts by giving me a example

argv parameter of main is a good and common example of a double pointer -- it is a two dimensional array of strings. It can be coded in two ways and the use of the argv is identical in both version.

int main( int argc, char **argv)
{
    // print the first string
    printf("%s\n", argv[0]);

   return 0;
}

or

int main( int argc, char *argv[])
{
    // print the first string
    printf("%s\n", argv[0]);

   return 0;
}

The other example where the double pointer is a pointer to a pointer, lets say you have a function foo() that allocates memory for a pointer that was declared in function main()

void foo( char ** ptr)
{
   *ptr = malloc(255); // allocate some memory
   strcpy( *ptr, "Hello World");
}

int main()
{
   char *ptr = 0;
   // call function with a pointer to pointer
   foo( &ptr );
   printf("%s\n", ptr);
   // free up the memory
   free(ptr);

   return 0;
}
Comments
Thanks for the example ,helps me alot

The other example where the double pointer is a pointer to a pointer, lets say you have a function foo() that allocates memory for a pointer that was declared in function main()

void foo( char ** ptr)
{
   *ptr = malloc(255); // allocate some memory
   strcpy( *ptr, "Hello World");
}

int main()
{
   char *ptr = 0;
   // call function with a pointer to pointer
   foo( &ptr );
   printf("%s\n", ptr);
   // free up the memory
   free(ptr);

   return 0;
}

end quote

What is the problem with this code if i do like this:

void foo( char * ptr)
{
   ptr = malloc(255); // allocate some memory
   strcpy( ptr, "Hello World");
}

int main()
{
   char *ptr = 0;
   // call function with a pointer to pointer
   foo( ptr );
   printf("%s\n", ptr);
   // free up the memory
   free(ptr);

   return 0;
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

>>void foo( char * ptr)

The problem is that ptr is a local object which has scope only within the function foo(). So the memory allocated with malloc() will be tossed into the bit bucket as soon as foo() returns to main(), and that will cause a memory leak. Inside function main() the value of ptr will still be 0 after foo() returns and the next line, printf(), will probably crash because the second argument (ptr) is a NULL pointer.

What is the problem with this code if i do like this:

Keep in mind one rule of thumb while using pointers in C and C++:

"To modify the value of a variable using pointers in a function, you need to pass the address of that variable".

This rule and is very well applicable to pointers as well.

But Why this works !!!!!

char* foo( )
{   
	char * ptr;
	ptr = (char*) malloc(255); // allocate some memory   
	strcpy( ptr, "Hello World");
	
	return ptr;
} 

int main()
{   
	char *ptr = 0;   // call function with a pointer to pointer   
	ptr = foo();   
	printf("%s\n", ptr);   // free up the memory   
	free(ptr);    
	
	return 0;
}

But Why this works !!!!!

That works because function foo() is not attemptig to modify a pointer that was declared in main(). It's perfectly acceptable for a function to return a pointer like it did in the code you posted.

Comments
He answers everything...right.

Hi gay
in ptr = foo(); you assign between 2 variable.
thinking
char *ptr = 0;
char *fun_ptr;
fun_ptr = ptr;
what is variable fun_ptr? fun_ptr is 0.

remember Pointers are variables.

But Why this works !!!!!

char* foo( )
{   
	char * ptr;
	ptr = (char*) malloc(255); // allocate some memory   
	strcpy( ptr, "Hello World");
	
	return ptr;
} 

int main()
{   
	char *ptr = 0;   // call function with a pointer to pointer   
	ptr = foo();   
	printf("%s\n", ptr);   // free up the memory   
	free(ptr);    
	
	return 0;
}

Hi, gay

you pass value not pass reference.

because the address of char *ptr in funcation main never is
cahnged. Don't think using function, run my code, you will understand what's wrong with you.

int main()
{
char *ptr = 0;
char *fun_ptr;
// call function with a pointer to pointer

fun_ptr = ptr;

printf("address of ptr %p\n", &ptr);
printf("address of fun_ptr %p\n", &fun_ptr);
printf("value of ptr %p\n", ptr);
printf("value of fun_ptr %p\n", fun_ptr);


printf("%s\n", ptr);
printf("%s\n", fun_ptr);

// only pass value which pointer variable
// no pss reference
// foo( ptr );

fun_ptr = malloc(255); // allocate some memory
strcpy( fun_ptr, "Hello World");

printf("%s\n", fun_ptr);

printf("%s\n", ptr);
// free up the memory
//free(ptr);

return 0;
}


What is the problem with this code if i do like this:

void foo( char * ptr)
{
   ptr = malloc(255); // allocate some memory
   strcpy( ptr, "Hello World");
}

int main()
{
   char *ptr = 0;
   // call function with a pointer to pointer
   foo( ptr );
   printf("%s\n", ptr);
   // free up the memory
   free(ptr);

   return 0;
}

But Why this works !!!!!

That works because foo's ptr, which is a local scope variable, is returned, which means the memory allocated for it won't get lost (it would still hold the correct value anyway, but this way we don't lose its address).

However, you're not really modifying main's ptr in foo(). You're doing that in main(), with the line

ptr = foo();

and that's why it works.

Note that this is what Ancient Dragon said, I just thought it could be broken down a little more.

Also, what ~s.o.s~ said translates in code to something like this, for updating a simple variable:

void foo(int*p)
{
    *p=3; //we equal the value at address 1000 to 3;
}
void main()
{
    int x; //memory for this is allocated, say at address 1000
    foo(&x); //we pass 1000 (x's address) to foo()
    printf("%d",x); //x is updated. 
}

and for a pointer:

void foo(int**p)
{
    int y = 5; //memory for this is allocated, say at address 3000, and that position of memory is filled with the value 5
    *p = &y; //we equal the value (contents) at address 2000 to 3000;
    /*note that we don't return the address of the int */
}
void main()
{
    int* p; //memory for this pointer (not for its contents!) is allocated, say at address 2000
    /* p points to nowhere right now */
    foo(&p); //we pass 2000 (p's address) to foo()
    /* p's value is now 3000, where an integer is stored */
    printf("%d",*p); //p is updated, its value (*p) is 5. 
}

Anyway, I know this adds nothing new to what was already said, but I think that having these somewhat more detailed examples, could help you or others figure this out. I was myself directed here because I had the exact same doubts as you did in the beginning. So thanks Ancient Dragon and ~s.o.s~ for the excellent answers.

Comments
Superb break down of double pointers
You're welcome. :-)

>>void foo( char * ptr)

The problem is that ptr is a local object which has scope only within the function foo(). So the memory allocated with malloc() will be tossed into the bit bucket as soon as foo() returns to main(), and that will cause a memory leak. Inside function main() the value of ptr will still be 0 after foo() returns and the next line, printf(), will probably crash because the second argument (ptr) is a NULL pointer.

No it wont crash because the pointer allocated in foo() is dynamically allocated & its life is not limited to foo's block.
Any other function can access the same dynamic memory space if the starting address of DM is known.
Thats why the code is working

Comments
a) you're wrong, as already explained, b) you're 3 years too late

this code is correct but when we r trying to input the string instead of copying it then it show es error.

@jadavbheda: What ever memory allocated won't get released by itself. The point is whether we have variable that points that memory or not. Here scope of the variable that matters.

In your code, the variable ptr lives and points to the allocated memory by foo().

Double "pointer", when you want to change value of <some_class>** but NOT of <some_class>*

So, we're basically preserving state of Memory location even outside function call.
Another use is like
for e.g. char* (as String), and char** (as Array of String)

Edited 3 Years Ago by Bharat_1: improved formatting

This article has been dead for over six months. Start a new discussion instead.