2

Let's say a have a pointer as a parameter, why doesn't it's value remain modified after the and of a function, and i have to use this syntax :

void function_name (int **p)
{
// code
}

and in main() :

int *v;

function name (&v);

I want to specify that i use a pointer to a struct type as a parameter.

Copacel
  • 1,328
  • 15
  • 25
  • 3
    You might want to share what's in `// code`. – FatalError Jun 24 '16 at 17:08
  • 1
    Similar to [this question](http://stackoverflow.com/questions/2229498/passing-by-reference-in-c). C is "pass-by-value" so any parameters you pass to a function have their values copied. However, even if a pointer address' value is copied, it still points to the same area in memory, meaning regardless of which copy of the pointer it is, changes made to the pointee will still be propogated to anyone who is pointing to the same thing. That said, we need to see what you're doing in `// code` to know what's going on... – Chris Sprague Jun 24 '16 at 17:08
  • 1
    can you give a more concrete code snippet? Note that pointers are passed by *value*, not reference, in C. – obataku Jun 24 '16 at 17:08
  • Depends on whether you wrote to `p` or `*p`. – John Bode Jun 24 '16 at 17:11
  • C11 draft standard n1570: *6.5.2.2 Function calls 4 An argument may be an expression of any complete object type. In preparing for the call to a function, the arguments are evaluated, and each parameter is assigned the value of the corresponding argument. 93) A function may change the values of its parameters, but these changes cannot affect the values of the arguments.* – EOF Jun 24 '16 at 17:12
  • Pointer as parameter is `int *p`, not `int **p`. The later is a pointer to a pointer, and is usually only used if you want to pass an array of pointers or if you want the function to modify the value of the pointer. – Havenard Jun 24 '16 at 17:18

2 Answers2

9

C passes arguments by value. If you want to modify something in a function and make the modification take effect in the calling function, a pointer to the variable in the calling function has to be passed. Otherwise, any changes made to the variable in a function are only local changes and does not affect the value of the variable in the calling function.

Let's start with an int type variable.

void foo(int x)
{
   x = 10;
}

int main()
{
   int a = 100;
   foo(a);  // Value of a does not change in this function
}

In the above program, the value of a remains 100 in main. The line

x = 10;

in foo only affects the value of the variable in foo. To make the change in foo affect the value in main, you'll need to pass a pointer to a.

void foo(int* x)
{
   *x = 10;
}

int main()
{
   int a = 100;
   foo(&a);  // Value of a changes in this function
}

Take that analogy to a pointer.

void bar(int* x)
{
   x = malloc(10*sizeof(int));
}

int main()
{
   int* ptr = NULL;
   bar(ptr);  // Value of ptr does not change in this function
}

bar allocates memory for an array of 10 ints and assigns the memory to x but that change is local. main does not see it. In main, ptr is still NULL. To make the change in bar affect ptr, a pointer to ptr has to be passed to bar.

void bar(int** x)
{
   *x = malloc(10*sizeof(int));
}

int main()
{
   int* ptr = NULL;
   bar(&ptr);  // Value of ptr changes in this function
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

In C, arguments are passed by value. This means that when you pass an argument to a function, a copy of that variable is made. For example

int main()
{
    int x = 6;
    repchar(x, 'r');

    printf("%d\n", x);
    return 0;
}
void repchar(int n, char c)
{
    while (--n >= 0)
        putchar(c);
}

This program prints the letter r six times, and then at the last printf, prints out 6, not -1. The reason is that when repchar was called, x was copied. That way, when repchar decrements n, the caller's copy is not changed.

If we passed a pointer, however, n would be modified.

int main()
{
    int x = 6;
    repchar(&x, 'r');

    printf("%d\n", x);
    return 0;
}
void repchar(int *n, char c)
{
    while (--(*n) >= 0)
        putchar(c);
}

Instead of the variable being copied, now the address of the variable is being copied. Inside of repchar, *n is being counted down. This accesses the value that is being referenced by n, which is the same address as x and decrements it. As a result, the last printf will give -1.

lost_in_the_source
  • 10,998
  • 9
  • 46
  • 75