pointer to pointer

This is a discussion on pointer to pointer within the C Programming forums, part of the General Programming Boards category; I have found in book that we can use a pointer to point to another pointer.So if we want a ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105

    pointer to pointer

    I have found in book that we can use a pointer to point to another pointer.So if we want a pointer p2 to point to another pointer p1 which points to an int then we should declare and use as **p2.Now these creates a chain of pointers.Although i had tried upto 4 steps of chaining,i want to know if this is reasonable and correct to do,or it causes any problem.I want to know a bit more about it because in book it is not elaborately explained.Any help and information about this topic is appreciated.I had done a simple program of chaining pointers.I also want to know if you people could be found in any chatroom or C programming Chatrooms.Please post a reply...
    The code of the simple program was:
    Code:
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
    	int x,*p1,**p2,***p3,****p4;
    	x=100;
    	p1=&x;
    	p2=&p1;
    	p3=&p2;
    	p4=&p3;
    	printf("x=%d                 \t&x(address of x)=%u",x,&x);
    	printf("\np1(address of x)=%u\t*p1(value at %u)=%u",p1,p1,*p1);
    	printf("\np2(address of p1)=%u\t*p2(value at %u)=%u",p2,p2,*p2);
    	printf("\np3(address of p2)=%u\t*p3(value at %u)=%u",p3,p3,*p3);
    	printf("\np4(address of p3)=%u\t*p3(value at %u)=%u",p4,p4,*p4);
    	printf("\n****p4(****&p3)=%d",****p4);
    	getch();
    	return 0;
    }

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    982
    First, to print out a pointer with printf() you should use %p, not %u (and to be especially correct, the argument should be cast to a void*).

    You can use multiple levels of indirection like that, but I can't ever recall a time I've seen a pointer to a pointer to a pointer being usefully employed. A pointer to pointer can be helpful for a few reasons. Sometimes it is used to simulate a 2d array (which means a pointer to pointer to pointer could be used to simulate a 3d array if you really happened to need one). Also, much like a pointer to, say, int, a pointer to a pointer is useful if you need a function to modify the pointer. Such as:
    Code:
    void f(int **p)
    {
      *p = malloc(5 * sizeof **p)
    }
    ..
    int *p;
    f(&p);
    Now p points to the allocated memory (or NULL if malloc() failed). If a pointer to pointer had not been used, the allocated value would have been stored in a local copy, and not modified in the caller. Look at the standard function strtol(), for example. It accepts a char** for this reason: it needs to be able to tell you where it stopped converting, and does so via a pointer to that particular character. So you pass in the address of your char*, and it points that char* appropriately.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by rakeshkool27 View Post
    I have found in book that we can use a pointer to point to another pointer.So if we want a pointer p2 to point to another pointer p1 which points to an int then we should declare and use as **p2.Now these creates a chain of pointers.Although i had tried upto 4 steps of chaining,i want to know if this is reasonable and correct to do,or it causes any problem.
    Well, greater and greater indirection is not really the purpose. Here's something laserlight showed me a while back, altho it may not be so meaningful to you rakeshkool, but it is kind o' funny:

    3 Star Programming

    Anyway, keep reading:

    Quote Originally Posted by cas View Post
    You can use multiple levels of indirection like that, but I can't ever recall a time I've seen a pointer to a pointer to a pointer being usefully employed.
    Gotta differ with that. They are a necessity, altho the frequency with which you use them is maybe a matter of style. Consider:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    void test (char **ptp) {
    	*ptp = malloc(12);
    	strcpy(*ptp,"hello world");
    }
    
    int main(void) {
    	char *ptr;
    	test(&ptr);
    	printf("%s\n",ptr);
    	return 0;
    }
    Okay, rakeshkool, what is so special about this? I'll tell you: because generally, if you want to assign something to a pointer inside a function body:
    Code:
    ptr = ANYTHING (such as malloc)
    If this pointer is to be used outside of the function, you must use the return value, because if
    Code:
    void test(char *ptr) {
         ptr = malloc(12);
    you do that, ptr NO LONGER REFERS TO THE PARAMETER SUBMITTED. In other words, if you rewrote the first code that way, printf("%s",ptr) would not print hello world. To do that, you have to do this:
    Code:
    char *test(char *ptr) {
            ptr = malloc(12);
            strcpy(ptr, "hello world");
            return ptr;
    }
    In this case, you would call it this way: ptr = test(ptr); -- sort of silly, you could just make it ptr = test() -- anyway, do you see a possible limitation?

    There is only one return value.

    So, using a pointer to a pointer, you can submit the address of a pointer (&ptr) and reassign it inside a function body. A lot of fairly standard functions actually work this way. This is just a rehash of what cas said, of course, dunno why s/he thinks it is not useful...

    NB: this does not involve more than two stars
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    982
    This is just a rehash of what cas said, of course, dunno why s/he thinks it is not useful
    I noted that I haven't seen pointers to pointers to pointers usefully used. Double indirection is absolutely a useful concept, of course.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Quote Originally Posted by MK27 View Post
    Gotta differ with that. They are a necessity, altho the frequency with which you use them is maybe a matter of style. Consider:
    FYI, cas mentioned a pointer to pointer to pointer (aka, 3 indirections), not pointer to pointer (2 indirections).
    Certainly, 2 are useful. 3 are less useful, or rare. That is not to say they aren't useful, but they are rarely employed.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Whoops! Sorry cas.

    I must be in overanxious "eager beaver mode" today.

    Hopefully the point(er) is made since we all seem to agree about this...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21