Here is an explanation:
int *p; //declares a pointer to an integer
int x; //declares an integer called x
int k; //declares an integer called k
//at this point all values are uninitialized and could contain anything
x = 30; //the numerical value 30 is assigned to x
k = x; //the contents of x(30) is assigned to k
p = &x; //the address of int x is stored in p
//now all values have valid and reliable values
Now let me explain what each line you described does:
k = 60; //assigns value 60 to variable k
*k = 60; //attempts to dereference k which is a non pointer type (compile time error)
p = 60; //attempts to assign the value 60 to a pointer p this is NOT what you want
*p = 60; //dereferences pointer p(which points to x) and sets the value 60 to it
the last line has the same effect as x=60;
so it is the correct answer.
k=60;
will not affect the content of x
, because x
and k
are different variables they each have their own separate memory. k=x;
will simply copy the contents of x
into k
, it will not make the variables aliases of each other.
3
solved Which statement will change value of x to 60 in C programing?