Passing by reference in C

Asked

Viewed 189 times

2

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

Take 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 this, if I wish to reference a pointer to a variable it is necessary to always pass the address of what I have in the scope of the function to ensure that the result is changed?

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

Is there another approach? How to facilitate this control?

  • 1

    What are you really trying to do? This is important to know whether you have to pass the address of the variable as a pointer, or the address of the pointer as a pointer, or the address of the pointer as a pointer pointer. If you get to a point where you need to manipulate pointer pointers, you’re probably doing something wrong. Usually, just one level of "ponteerism" and rarely two.

  • I had a college job where we implemented lists and queues, so basically it passes a pointer to the first function, that is we had **p, if within this function I wanted to modify a die from my queue I would have to pass the pointer address? Or just passing the pointer pointer would be enough?

  • The secret to ensuring the passage of parameters by reference is to be a pointer to what Voce originally assigned?

  • My question is if in a certain scope (function) I need to pass a structure I have there (in the case it was received as an argument of such a function) to another function by reference, I need to send the address of what I received by argument? that is, I received *p , will I have to pass the pointer to that structure? In this case **p?

2 answers

2


  1. You need to malloc() before using the pointer.
  2. You don’t always need to add a level of indirect to each new function: just send the address the first time and then reuse this address.
  3. Object declaration follows use: int *a means that *a is a int; int *****b means that *****b is a int.

If you want to put a value on a type object int *** uses ***a.

Look at 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;
}
  • When you get home I will be, but in case the value will be the same, right? Thank you for answering.

2

You don’t need to use pointer to pointer, just use a "simple" pointer. To understand more about pots, I suggest these other two questions:

I redid your code so it works the way you want it to:

#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 a pointer to an integer as a parameter (int *a). The function foo simply forward it to the other (because its parameters have the same type of data). Note however:

  1. In function main I did not declare a pointer, but a "normal" variable. In the call foo(&a) I use the operator & to indicate that I want to pass there the address of the variable a.
  2. In function bar i update the contents of an area "pointed" by a pointer by doing the following: *a = 1. This basically means "assigns 1 to the memory area pointed by a".

I mean, whenever you see &algo you’re picking up 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).

P.S.: I reversed the order of the functions so the compiler doesn’t complain about the functions not being declared when used.

  • Thank you for the answer, but in case you have a pointer on the Main, in the first function step a pointer to the pointer, inside this function I want to call another so that also modifies this structure, as I already have a pointer pointer, step like this? Or do I have to pass the pointer pointer address? I hope you understand

  • Yes, you can, by doing **a=1, for example. If you already have a pointer to the pointer and want to "pass it on" to another function that already receives this type of data, just make the call bar(a) normally. In fact, everything I described remains valid, just imagine that **a is the equivalent of *(*a). If you still have doubts, after the two answers you have already had, I strongly suggest you read the other questions I mentioned and do some exercises (printing the values of a, &a and *a). It’ll help you out a little bit.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.