It is said that variables in a function cannot be changed by another function. Only by using pointers can variable values be changed.
I am writing some functions to try to prove this theory, but I can't get it right.
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int x = 10;
printf("default x value is %d\n",x);
try1(x);
printf("x after try1 is %d\n", x);
try2(&x);
printf("x after try2 is %d\n", x);
return 0;
}
Code:
#include <stdio.h>
void try1(int x){
printf("x in try1 is %d\n", x);
x++;
printf("x in try1 after ++ is %d\n", x);
}
void try2(int *x){
printf("x in try2 is %d\n", *x);
*x++;
printf("x in try2 after ++ is %d\n", *x); // Error, always gives me the address memory number
}