C Programming - Expressions - Discussion

Discussion Forum : Expressions - Yes / No Questions (Q.No. 3)
3.
Will the expression *p = p be disallowed by the compiler?
Yes
No
Answer: Option
Explanation:
Because, here even though the value of p is accessed twice it is used to modify two different objects p and *p
Discussion:
9 comments Page 1 of 1.

Himanshu goel said:   1 decade ago
Please illustrate it with an example.

Thanks.

Wikiok said:   1 decade ago
int a=10;
int p*=&a;
*p=p; // *p = the address stored in p other words where p points; p=&p= address of p itself.
So after that p will point to the memory location "10".

Shashanka said:   1 decade ago
Can't understand please explain me more clearly.

Dheeraj said:   1 decade ago
SAMPLE RUN ON LINUX

#include <stdio.h>
main()
{

int a=10;
int *p =&a;
printf("%d\n",*p);
printf("%d",p);

printf("\nafter assign\n");
*p=p;
printf("%d\n",*p);
printf("%d",p);

}

here after assign both p and *p will have the same value of the address of p;


and on Dev cpp it is giving error..
invalid conversion from `int*' to `int'

Swagath9849636443 said:   1 decade ago
Wikiok and Dheeraj ! both are correct. The program also easily understandable. Both *p and p values are same i.e. address of itself i.e. p.

Shweta said:   1 decade ago
@Dheeraj.

I think it is the address of a not the the address of p, as the value of p is the address of a.

Vinod said:   9 years ago
p is a pointer variable, whereas *p evaluated as (*(&a)) = (a) is a normal variable. How both have same values?

Rekha said:   4 years ago
Yes, agree with you @Vinod.

Anyone please explain it.

Hossam ali said:   3 years ago
@All.

Example code to understand it easily.

#include <stdio.h>
#include"test.h"


int main(void)
{
int a=10;
int *p=&a;
printf("%d\n", &p);
printf("%d\n", p);
printf("%d\n", *p);
*p=p;
printf("%d\n", &p);
printf("%d\n", p);
printf("%d\n", *p);
return 0;
}

o/p is
6356744
6356748
10
6356744
6356748
6356748.

Post your comments here:

Your comments will be displayed after verification.