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?
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.
– Victor Stafusa
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?
– Fernando Medeiros
The secret to ensuring the passage of parameters by reference is to be a pointer to what Voce originally assigned?
– Fernando Medeiros
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?
– Fernando Medeiros