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 2 of 4.
Lavanya said:
1 decade ago
What is char P?
Aiswarya said:
1 decade ago
Please tell me whether p is a character constant or simply a constant.
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)
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*/
}
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.
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
Chery said:
1 decade ago
P is a pointer to constant of type char.
Atul said:
1 decade ago
P is the pointer to char constant
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.
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.
Rupinderjit said:
1 decade ago
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers