C Programming - Typedef - Discussion

2. 

In the following code what is 'P'?

typedef char *charp;
const charp P;

[A]. P is a constant
[B]. P is a character constant
[C]. P is character type
[D]. None of above

Answer: Option A

Explanation:

No answer description available for this question.

Jothi Basu said: (Aug 14, 2010)  
P is a character constant using pointer.

Ashok G said: (Aug 20, 2010)  
I guess P is a pointer to a const char.

Vinay said: (Oct 18, 2010)  
P is a character constant.

Shweta Dubey said: (Oct 26, 2010)  
P is a pointer to a constant

Kumaran said: (Nov 14, 2010)  
P is the constant... It defined in the outside of main function.

Sunil said: (Dec 4, 2010)  
here p is constant bcz of usage of keyword const

Santhosh said: (Jan 31, 2011)  
her p is a pointer to a constant character

Hngn said: (Feb 8, 2011)  
p is constant chracter pointer

Vikas Sharma said: (Feb 16, 2011)  
P is a constant pointer.

Ana said: (Mar 19, 2011)  
The answer is no clear to me. Can some one please explain.

Wikiok said: (Apr 10, 2011)  
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;

Lol said: (May 6, 2011)  
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.

Rocky said: (Jul 24, 2011)  
p is constant or p is character constant???
please answer!!!!

Ravi said: (Sep 21, 2011)  
P is a pointer to a constant.

Because,

Const char *p;.

Ganga said: (Dec 4, 2011)  
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;

Asd said: (Dec 12, 2011)  
I think finally answer is p is a pointer to a character constant which is not in option so anwer is d.

Rupinderjit said: (Jan 5, 2012)  
p can't be pointer, since with typedef we can't modify the actual base type,we just only give new name to existing type. That's all folks.

Lijina said: (Jan 24, 2012)  
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.

Atul said: (Jun 4, 2012)  
P is the pointer to char constant

Chery said: (Jul 1, 2012)  
P is a pointer to constant of type char.

Tulsiram said: (Oct 13, 2012)  
typedef char *charp=> charp is character pointer
but
const charp P => P it is simply constant

Deepak Agrawal said: (Mar 3, 2013)  
P is a constant because it defines.

And it's a type of pointer type.

Adetunji David said: (Aug 26, 2013)  
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*/
}

Ravi said: (Dec 14, 2013)  
const p;

The syntax itself having the keyword "const" which indicates the constant values.

Aiswarya said: (Dec 14, 2014)  
Please tell me whether p is a character constant or simply a constant.

Lavanya said: (Jan 2, 2015)  
What is char P?

Niky said: (May 3, 2015)  
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.

Anis Gupta said: (Jul 23, 2015)  
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).

Muneesh said: (Sep 12, 2017)  
How is this possible?

Venkatesh said: (Oct 2, 2017)  
Can someone explain clearly?

Trishul said: (Jan 11, 2018)  
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.

Som said: (Jan 27, 2018)  
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.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.