C Programming - Typedef - Discussion

Discussion Forum : Typedef - General Questions (Q.No. 2)
2.
In the following code what is 'P'?
typedef char *charp;
const charp P;
P is a constant
P is a character constant
P is character type
None of above
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
36 comments Page 1 of 4.

Mladen Saldanha said:   5 months ago
@All.

typedef char *charp;
This defines charp as a pointer to char (equivalent to char*).
const charp P;

This declares P as a constant pointer to char (not a pointer to a constant char).
The const applies to the pointer itself, not the data it points to.
So, it should be none of the above.

Jayesh said:   4 years ago
I agree with you @Santosh.

Jethalal said:   5 years ago
@Pooja No, I think it should be a pointer to a character constant only.
(1)

Pooja said:   6 years ago
I think p is constant only.

Som said:   8 years ago
Can anyone explain how the const charp P is expanded?

If it is expanded as const char* P, then it means it is a pointer to a character constant.
Hence ++P must be allowed and (*P)++ must not be allowed.
But while compiling the error is thrown for ++P saying that P is a constant and (*P)++ is error free.

Trishul said:   8 years ago
Here charp is pointer to a character type.
Where as p is a constant of charp type not char type.
There is no option which says this.
The only option is the first one which tells it is a constat.

Venkatesh said:   8 years ago
Can someone explain clearly?

Muneesh said:   8 years ago
How is this possible?

Anis gupta said:   1 decade ago
So simple dear, typedef is type replacement means:

charp replace with char * so it becomes.

const char *P; (pointer always reads from left to right).

Value can't change (pointer to character constant).

Niky said:   1 decade ago
Why p is not character constant here.

Because according to def of typedef it will change name from char* to chptr then it will be const char*p.


Post your comments here:

Your comments will be displayed after verification.