This code snippet has undefined behavior
int x;
int *p;
x=3;
*p=&x;
printf("x= %d\n",x);
printf("p= %d\n",*p);
because the pointer p
was not initialized and has indeterminate value that does not point to a valid object. So this statement
*p=&x;
by dereferencing the pointer ( *p
) is trying to access the memory at an invalid address.
It seems that instead this statement
*p=&x;
you mean
p = &x;
that is the pointer gets the address of the variable x
.
Pay attention to that the outputted message of such a call of printf
printf("p= %d\n",*p);
is confusing. It would be better to write
printf("*p= %d\n",*p);
That is it is not the value of the pointer itself that is outputted but the value of the pointed object that is outputted.
You should distinguish the meaning of the asterisk in a declaration and in an expression.
In a declaration like this
int *p=&x;
the asterisk means that here is declared a pointer and it gets the address of the variable x.
In an expression statement like this
*p=&x;
the asterisk denotes the dereferencing operator that is used to access the pointed object by means of the value stored in the pointer used as an address.