How can we change pointer via function call

Hi

I have a general c programming question.

ptr variable point to memory of x variable

void main () 
{
    int x = 10;
    int *ptr = &x;
    
   foo(&ptr) ;

}

What i need to do in function foo If I want to change value of ptr via function call.

How can we change pointer via function call?

If you want a function to be able to change the value of an argument passed to a function, use pass by reference.

But what you wrote seems odd, you are not passing the pointer, you are passing the address of the pointer.

This might be what you meant, and is indeed one way of being able to pass by reference, the way it has to be done in old regular C.

Please put the problem in a larger context so we can know what you are trying to do. Or learn.

a7

1 Like

besides passing an argument by reference (e.g. int &i), it is common to pass a pointer (int *p).

on the other hand, you may want to actually change the value of a pointer, for example a pointer to a string and want it to point to a different string. in that case you need to pass a pointer to the pointer (char **p) so that now instead of the string being "red", it is "green".

(of course that function could simple return a pointer to a different string (e.g. char *p = func(someArguments);)

1 Like

I am trying to learn how we can change the value of pointer through function call.

We can pass the parameter by reference or by value. Pass by value means, the copy of the variable value is passed to the function.

The ptr variable in the code stores the memory location of the x variable. I want to create a function that when called will change the value of the ptr.

I think that the pointer tells where in memory the 'x' lives. If you change the pointer value, it will not find 'x' anymore, and will return some other value.

You were already provided with the answer:

Yes. It is not clear what @kittu20 is after, or confused about.

Taken literally, this is what I came up with:

# include <stdio.h>

void foo(int **k)
{
    (*k)++;
}

int main(void) 
{
    int x = 10;
    int *ptr = &x;
    
    printf("pointer is %p\n", ptr);
    
    foo(&ptr);
    
    printf("pointer is now  %p\n", ptr);
    
    return 0;
}

This was the output
pointer is 0x7fff4522668c
pointer is now  0x7fff45226690


...Program finished with exit code 0
Press ENTER to exit console.

HTH and beware of messing with pointers even when you do know what you are doing!

a7

that's what i was looking. You pass a pointer to a pointer, i am trying to figure out why not pass only pointer


void foo(int *k)
{
    (*k)++;
}

int main(void) 
{
    int x = 10;
    int *ptr = &x;
    
    printf("pointer is %p\n", ptr);
    
    foo(&ptr);
    
    printf("pointer is now  %p\n", ptr);
    
    return 0;
}

I am trying to figure out why compiler gives warning when passing pointer only

if you pass the pointer, it is the value of the point, for example, the address of x. if you change its value in the functoin, you are only changing the local argument's value (on the stack), not the value of the pointer from which the value taken and passed as an argument to the function.

Heed warnings.

In C/C++ the compiler will warn you when you are doing something that might be wrong.

It is wrong to pass a pointer to a function expecting a pointer to a pointer.

A warning may have meant "think about that - do you really want to <whatever>?".

TBH I can't test it now, I would have thought that to be a full-on error.

It's the same as any argument. You made a promise in the declaration to the function, and broke that promise when you used it.

a7

Why not simply:

int *foo(int *k)
{
    return ++k;
}

int main(void) 
{
    int x = 10;
    int *ptr = &x;
    
    printf("pointer is %p\n", ptr);
    
    ptr = foo(ptr);
    
    printf("pointer is now  %p\n", ptr);
    
    return 0;
}

The OP wanted the function to modify the argument, not return a value to be assigned.

I assume this is some kind of learning exercise.

a7

void functions don't return a value.

so define the function to return the value of a pointer

Fixed.

what you are passing when you say void foo(int **k)

what you are passing when you say "void function foo(int *a);"?

Pointer to a pointer to an int.

Pointer to an int.

A short, enlightening, read:
References vs. Pointers - Embedded.com

I don't think you should say that. You mean

    void foo(int *a);

a7

every variable is located at a specific location (address) in memory. a pointer is a variable with the value of an address.

foo (int x) is passed the value of "x". it is passed on the stack. changing x with foo() simply changes the value on the stack

foo(int *x) passes the address of "x" on the stack. the stack value is typically never modified. instead code with foo() will change the value of x from the calling code using the pointer, *p = 123. if "x" is located at address 0xFAB1000, that is the value passed to the function and used to change the value of "x".

pointer "p" could be set the the address of "x", int *p = &x;. if "x" is located at 0xFAB1000, that will be the value of p. but "p" may be located at 0xFAB20C0. if the the address of p is passed to foo(), foo (int **p), then its address, 0xFAB20C0 is passed to the function allowing the value of "P" to be modified in the scope of the calling function