I have a question in C. Consider the following code (which is a minimal example):
#include <stdio.h> int f(int**, int*); int main(int argc, char *argv[]) { int *u = NULL, t1=0, t2=1; u = &t1; printf("t1 : %d\n", t1); printf("t2 : %d\n\n", t2); *u = 36; printf("t1 : %d\n", t1); printf("t2 : %d\n\n", t2); *u = f(&u, &t2); printf("t1 : %d\n", t1); printf("t2 : %d\n\n", t2); return 0; } int f(int** p, int* e){ *p = e; return 24; }
When I run this program, I get the following result:
t1 : 0 t2 : 1 t1 : 36 t2 : 1 t1 : 24 t2 : 1
What surprises me is that the left side of the expression (i.e. * u):
*u = f(&u, &t2);
fixed before processing the function f. In fact, I was expecting the following result, since the function f changes the pointer u:
t1 : 0 t2 : 1 t1 : 36 t2 : 1 t1 : 36 t2 : 24
This is normal? Am I missing something in my class C?
source share