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.

Adetunji David said:   1 decade ago
P is a constant pointer.

'const charp p' doesn't translate to 'const char* p'

Think of it this way:

const int x;//means x is an integer constant.
const charp p;//means p is a charp constant.

The purpose of typedef is to form complex types from more-basic machine types and assign simpler names to such combinations.[wikipedia]

It doesn't just carry out string substitution like #define Macros.

C program to buttress my point.

#include<stdio.h>
int main()
{
typedef char *charp;
const charp p="ABCD";
p="XYZ";
printf("%s",p);
return 0;
/*output is a compiler error stating that p is read-only.
so assigning "XYZ" is illegal*/
}

However,

#define charp char*
#include<stdio.h>
int main()
{
const charp p="ABCD";
p="XYZ";
printf("%s",p);
return 0;
/* executes fine. output is XYZ*/
}

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.

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.

Lijina said:   1 decade ago
Option A is Right,

1. typedef char *charptr;

So charptr contain one address and *charptr contain any constant;

2. const charptr p;

If p='a' and &p=0x10;
Now charptr contain &p(0x10)

Only for address we are keeping const, not for value in that address. So P that is constant.

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).

Wikiok said:   1 decade ago
typedef char *charp;
const charp P;

so:
const char *P; // The value can not be changed at that memory location, but other memory locations can be used for *P.
*P=4; //ERROR
P=&a; //Works char a;

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.

LOL said:   1 decade ago
The trick with const modifier is to read the statement backwards.

const charp P = const char *P;

So reading backwards you have P * char const
which is P is a pointer to a char constant.

Ganga said:   1 decade ago
const char *P; // The value can not be changed at that memory location, but other memory locations can be used for *P.
*P=4; //ERROR
P=&a; //Works char a;

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.