I\'m trying understand how to pass a parameter by reference in C language. So I wrote this code to test the behavior of parameters passing:
#include
Actually not really much a difference, except the first one is broken. :) (Well, both are, but the first is broken more).
Let me explain what happens in the second case:
n of type pointer-to-int is allocated on the stackint is allocated to the stack, it's address is stored in variable nalocar is called, being passed the copy of variable n, which is the copy of the address of our variable of type intint variable being pointed by n to 12n (12)The first case:
n of type pointer-to-int is allocated on the stackalocar is called with a copy of the variable n (which is still uninitialized - contains an unknown value)int is created in memory and the local copy of variable n in function alocar is set to point to that new variablen) is set to 12 and printedn variable in main is still uninitialized, it points to a random place in memory. So the value in random place in memory is printed (which is likely to crash your program).Also, both programs are broken because they don't free the memory allocated by malloc().