I have a function as follows:
void foo (int *check){
*check= 9;
printf("*check: %d\n",*check);
//when I print "*check" here, the value changes as 9.
}
This is the main function.
void main () {
int check=5;
foo(&check);
printf("check: %d\n",check);
//when I print "check", gives me 5.
}
I want to change the value of "check" variable but it does not work. Where am I making mistake? Thank you!
I am using makefile while running it. Edit: malloc is deleted now it gives me Segmentation fault (core dumped) error
You are doing everything just fine, the only thing that is wrong is trying to
mallocthe variable you passed to the function , since it is already allocated on stack. In order to do what you're trying to do, you should declare the integer as pointer to integer (int *check) outside the function, and avoid using the&character when calling the function with the pointer as parameter.