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;
Discussion:
36 comments Page 3 of 4.
Tulsiram said:
1 decade ago
typedef char *charp=> charp is character pointer
but
const charp P => P it is simply constant
but
const charp P => P it is simply constant
Deepak Agrawal said:
1 decade ago
P is a constant because it defines.
And it's a type of pointer type.
And it's a type of pointer type.
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*/
}
'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:
1 decade ago
const p;
The syntax itself having the keyword "const" which indicates the constant values.
The syntax itself having the keyword "const" which indicates the constant values.
(1)
Aiswarya said:
1 decade ago
Please tell me whether p is a character constant or simply a constant.
Lavanya said:
1 decade ago
What is char P?
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.
Because according to def of typedef it will change name from char* to chptr then it will be const char*p.
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).
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:
8 years ago
How is this possible?
Venkatesh said:
8 years ago
Can someone explain clearly?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers