Hi! I am a new beginner to coding and I'm sorry if this is a bad question, but why is there a pointer delclared in the uupdate function when there is already a pointer in the main function?
#include
void update(int *a,int *b) {
// Complete this function
}
int main() {
int a, b;
int *pa = &a, *pb = &b;
scanf("%d %d", &a, &b);
update(pa, pb);
printf("%d\n%d", a, b);
return 0;
}
Answers
Answer:
This is called the call by reference method.
Explanation:
Your doubt is valid, everyone gets confused at this part. I did too. If you don't already know, call by reference passes the addresses of variables as a function's arguments and any modification done to the variable copies in the function through their address will alter the original variables.
As for your doubt,
The arguments passed into the function (pa and pb) are essentially pointers carrying the addresses of variables a and b defined in main(). So naturally, we need to declare the function parameters as pointers too. That is, we are letting the program know that the function is receiving addresses as its arguments. You can't equate a pointer to an ordinary variable, it should be a pointer for a pointer.
Hope that clarifies your doubt ^^