Passing by reference in C

2

In the case of C language, when we want to pass a variable by reference we need to pass the address of the function to the function, I have a question in this case.

See the example:

int main(){
   int *a;
   *a = 5;
   foo(&a);
}


void foo(int **a){
 //tenho que passar o endereço para garantir a mudança?
   bar(&a);
}

void bar(int ***a){
 //como atribuir? ***a = 1; ??
}

My question is as follows, if I want to pass a reference to a pointer to a variable, is it always necessary to pass the address of what I have in the scope of the function to ensure that the result is changed?

If yes, how do I assign the value when I have **a or ***a ?

Is there another approach? How to facilitate this control?

    
asked by anonymous 03.07.2015 / 19:12

2 answers

2
  • You need malloc() before using the pointer.
  • You do not need to always add a level of indirection to each new function: just send the address the first time and then reuse that address.
  • Object declaration follows usage: int *a means *a is int ; int *****b means *****b is int .
  • If you want to put a value in an object of type int *** it uses ***a .

    See this code

    #include <stdio.h>
    #include <stdlib.h>
    
    void foo(int **a, int *b);
    void bar(int ***a, int *b);
    
    int main(void) {
        int *a;
        int b;
        a = malloc(sizeof *a);
        if (a) {
            *a = b = 5;
            printf("antes de foo(): %d, %d\n", *a, b);
            foo(&a, &b);
            printf("depois de foo(): %d, %d\n", *a, b);
            free(a);
        }
    }
    
    
    void foo(int **a, int *b) {
        // tenho que passar o endereço para garantir a mudança?
        printf("antes de bar(): %d, %d\n", **a, *b);
        bar(&a, b);
        printf("depois de bar(): %d, %d\n", **a, *b);
    }
    
    void bar(int ***a, int *b) {
        ***a = *b = 42;
    }
    
        
    03.07.2015 / 19:29
    2

    You do not need to use pointer to pointer, just use a "simple" pointer. To understand more about potters, I suggest these two other questions:

    I have refactored your code to work as you wish:

    #include <stdio.h>
    
    void bar(int *a){
        *a = 1;
    }
    
    void foo(int *a){
        bar(a);
    }
    
    int main(){
       int a;
       a = 5;
       foo(&a);
       printf("a = %d\n", a);
       return 0;
    }
    

    Both functions expect as a parameter a pointer to an integer ( int *a ). The foo function simply passes it to the other (because its parameters have the same data type). Note however:

  • In the main function I did not declare a pointer, but a "normal" variable. In calling foo(&a) I use the & operator to indicate that I want to pass the address of the a variable there.
  • In the bar function I update the contents of a "pointed" area by a pointer by doing the following: *a = 1 . This basically means "assign 1 to the memory area pointed to by a ".
  • That is, whenever you see &algo you are getting the address of the memory area where algo is allocated. Whenever you see *algo you are actually accessing this address (both to read and to change).

    Q.: I've reversed the order of functions so the compiler does not complain about the functions not being declared when used.

        
    03.07.2015 / 19:29