1

I have a case that I need address of a variable in a pointer. The variable is located in different file, hence I created a function and passing it a pointer. The function assigns address of variable to pointer.
But address of that variable in not updating in pointer. My code is as follows --

typedef struct
{
    int* ptr;
} test;

int sGlobalVar = 10;
test GlobalStruct; //Create instance of struct


//This function address of Global variable to passed pointer
void GetAddress(int* ptr)
{
   ptr = &sGlobalVar;
   //Prints correct value
   printf("Value of Global Variable in Function %d\n", *ptr);
}


int main()
{

    printf("Hello World!!");
    GetAddress(GlobalStruct.ptr);

    // CODE CRASHES HERE. Because GlobalStruct.ptr is NULL
    printf("Value of Global Variable in Main %d \n", *GlobalStruct.ptr);

    return 0;
}

Next thing I did is modified my function GetAddress() such that it accepts pointer to pointer.

//This function address of Global variable to passed pointer
void GetAddress(int** ptr)
{
   *ptr = &sGlobalVar;
   //Prints correct value
   printf("Value of Global Variable in Function %d\n", **ptr);
} 

and main as

 int main()
    {

        printf("Hello World!!");
        GetAddress(&GlobalStruct.ptr);

        //Now Value prints properly!!
        printf("Value of Global Variable in Main %d \n", *GlobalStruct.ptr);

        return 0;
    }

I am clueless why First method is not working.

5
  • Pointers, like everything else in C, are passed by value expression. All your doing in the first case is modifying the value held by what is effectively a local, automatic variable. If you want to modify something from the caller side a dereference of some kind is required, and that requires a pointer to the type you're dereferencing, even if it's already a pointer type (thus your pointer-to-pointer works). Ex: you want to modify a caller-side int, pass int* and deference with *p = ...`. The same is true for pointers. Commented Dec 6, 2016 at 9:29
  • The printf function has a %p format specifier that you should have a play with to find our where the various things live. It is used the same as %d except that it expects a void* argument (you can use any old pointer). Try pasting printf("The address of ptr is %p",&ptr); around your code. Try it with other variables too and get some ideas about the memory management. When you use the heap (malloc, free, etc.) you'll find even more memory regions pop up. (Maybe even try passing it a function name, or a label value!) Commented Dec 6, 2016 at 9:48
  • @MikeofSST It wasn't problem with printf, See the answers. Commented Dec 6, 2016 at 9:54
  • @Swanand I know, which is why I posted as a comment. It was a suggestion to use the %p pointer specifier to see what the other answers said for yourself, so that you can see the difference in the addresses between stack and static variables. Even use it to look at the value contained in ptr. Commented Dec 6, 2016 at 9:57
  • @MikeofSST Oh... Very sorry... I interpreted it incorrectly... Got your point! Commented Dec 6, 2016 at 10:48

2 Answers 2

2

The first method doesn't work as you are passing the pointer by value and updating it. In the second method you are passing it by reference, so the value updated stays.

To put it simply, when you do a pass by value, the caller and callee have 2 different copies of the variable, so data updated by the callee is not reflected in the caller. Whereas in pass-by-reference, this is not the case, the data updated reflects in the caller.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah!! Got it!! To pass the pointer by reference, I need to pass it's address... Hence double pointer!! Thanks a lot!!
1

The call GetAddress(GlobalStruct.ptr); in the first version of main() does not change the value of GlobalStruct.ptr in the caller.

The pointer is passed by value.

(The second way works since you are changing the value of GlobalStruct.ptr in the caller as you are passing a pointer to the pointer).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.