I am trying to pass a struct by reference through multiple functions but I am getting garbage value when I try to print the value of the struct in each function. How to correctly pass it by reference, so that the value is passed back until from func3() -> func2() -> func1()-> main() function
typedef struct _buf {
void *ptr;
size_t len;
} buf;
int func3(buf *x){
x->ptr = 45;
printf("func3 %s \n", (char *)x->ptr);
return 0;
}
int func2(buf *x){
func3(&* x);
printf("func2 %s \n", (char *)x->ptr);
return 0;
}
int func1(buf *x) {
func2(&*x);
printf("func1 %s \n", (char *)x->ptr);
return 0;
}
int main() {
buf x = {NULL, 0};
func1(&x);
printf("main %s \n", (char *)x.ptr);
return 0;
}
45- what do you expect the output to be?func2(&*x);->func2(x);x-ptrinfunc3you are asking to see what is at memory location45, which almost certainly will cause an exception/