ptr
is the actual pointer, while *ptr
is whatever it is pointing at, so *ptr=&var
does not really make any sense, unless it’s a pointer to a pointer. It’s either ptr=&var
or *ptr=var
If you really want to assign a variable to a pointer, it is possible with casting. This compiles, but I cannot see any good reason to do something like this at all:
#include <stdio.h>
main()
{
int var=4;
int *ptr;
ptr = (int *)var;
printf("%d\n", *ptr);
}
The behavior is undefined. When I ran it, it segfaulted.
5
solved C: Whats the difference between pointer = variable and pointer = &variable?