Usage difference between pointer and double pointer ( pointer to pointer)

This is a discussion on Usage difference between pointer and double pointer ( pointer to pointer) within the C Programming forums, part of the General Programming Boards category; Hello friends, I am using a library API , to this API iam sending a pointer variable in below two ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    112

    Usage difference between pointer and double pointer ( pointer to pointer)

    Hello friends,

    I am using a library API , to this API iam sending a pointer variable in below two way

    first way

    char *ptr = NULL;

    func(ptr);

    second way is

    funct(&ptr);

    while using the first implementation i have allocated some local buffer in my lower level function in that library API and assigend as below

    char *testimg = NULL;

    testimg = malloc(sizeof(char) * 1024);
    char data[] = "hdfdfd...."; /* This is of total size occupying 1024*/
    memcpy(testimg,data,1024);

    here if i do ptr = testimg

    and print ptr it gives proper result in that function , but if i go to upper level funcitons / to the source program this ptr shows me as null .

    In second implementation if i change the statements as below

    *ptr = testimg

    This gives me correct results in all uppler layer and source file .

    How is this successfull when compare to the first implementation failure ?


    Thanks in advance

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I don't know what it is you're trying to explain, but let me do a little explaining:

    If you want the value a variable has before a function call, to be changed by the function call, then you need a pointer to that object. It doesn't matter what the type is.
    Code:
    int x;
    
    changex( &x ); /* pass the address of x to a function so the value x has can be changed */
    The same thing goes for a pointer. If you want a function to be able to make that pointer point at something else (change its value), then you need to pass the address of that pointer, not the pointer's value.

    Pointers are just like every other variable: they store a value. The value just happens to be a memory address.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    112
    Here my concern was that the ptr is showing the buffer when it is the scope of the fucntion which is allocating the memory . But when i am trying to see the buffer / access it in the calling functions / upper level function i could not do the same.

    To some extent i understood the reason behind it by your views.

    My main confusion was that pointer points to some memory address so when i am pointing that to some other locations , it should contain the same value even after it comes out of that scope of the function ( even though iam passing the pointer as just value i.e ptr , rather that &ptr).

    Hope iam making sese compare previous posting.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's the same as assigning a value to an int inside a function, and not having it keep. You are passed the value of a pointer ... just a memory address. You cannot change the value of that pointer; make it point to another memory address, unless you actually have a pointer-to-a-pointer. Consider:
    Code:
    void foo( int x )
    {
        x = 5;
    }
    The way this function works, is it is passed an integer (value), which is stored in a temporary varible x when the function is called. We can set that temporary variable's value to 5, but we cannot set the actual variable passed, to five.

    The same thing happens to your pointer:
    Code:
    void bar( int *x )
    {
        x = malloc( sizeof( int ) );
    }
    The value of a pointer, a memory address, is passed to this function, and stored in a temporary pointer variable, named x. The value that the temporary variable x holds, can be changed, but you cannot change the original pointer (from where the function was actually called), because you do not have a pointer to it.

    This is clear, once you understand that pointers are just variables which hold values:
    Code:
    foo( 3 );
    As with our first example, this illustrates that it is really just a value being passed, and not a variable, because you can't change 3's value, because 3 is not really a variable. It's a value. Even if you call it like this, it's still just a value being passed:
    Code:
    int y = 2;
    foo( y );
    This still just passes the value that y has to the function.

    Everything in C is passed as a value. The only thing points do is represent the value you pass as a memory address, which can then be dereferenced to get or change what is located at that address. But it's still just a value being passed. Which is why when bar tries to malloc a new int, it doesn't work. You don't have the address of the pointer. You have the value stored in the pointer; just like how y doesn't really pass y, it passes the value stored in y, in the last code segment above.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 04:45 AM
  2. unable to read double A[0] and A[1] when n=1
    By sweetarg in forum C Programming
    Replies: 2
    Last Post: 10-25-2005, 01:35 PM
  3. Replies: 8
    Last Post: 03-10-2005, 08:14 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

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