C Programming - Typedef - Discussion
Discussion Forum : Typedef - Yes / No Questions (Q.No. 2)
2.
Is there any difference in the #define and typedef in the following code?
typedef char * string_t;
#define string_d char *;
string_t s1, s2;
string_d s3, s4;
Answer: Option
Explanation:
In these declarations, s1, s2 and s3 are all treated as char*, but s4 is treated as a char, which is probably not the intention.
Discussion:
9 comments Page 1 of 1.
Sundar said:
1 decade ago
In the case of typedef:
string_t s1, s2;
This will be interpreted as
1. char *s1;
2. char *s2;
But in the case of pre-processor:
string_d s3, s4; <-- In this line 'string_d' will be replaced with 'char *'.
So, it will become
char *s3, s4; <-- Here watch carefully, s4 does't have '*'.
Therefore, s4 will be treated as 'char' only.
Note: #define preprocessor simply works like find-n-replace method.
string_t s1, s2;
This will be interpreted as
1. char *s1;
2. char *s2;
But in the case of pre-processor:
string_d s3, s4; <-- In this line 'string_d' will be replaced with 'char *'.
So, it will become
char *s3, s4; <-- Here watch carefully, s4 does't have '*'.
Therefore, s4 will be treated as 'char' only.
Note: #define preprocessor simply works like find-n-replace method.
(3)
Palani said:
1 decade ago
Please explain anyone properly
(1)
Akshat said:
8 years ago
@Sundar
Tell me one thing if you replace "string_d" with "char*" then it will be like "char* s3,s4"
that is equal to char* s3;
char* s4;
So why are you saying that there is no "*" with s4, s4 didn't need any start as it's been used is keyword itself.
Tell me one thing if you replace "string_d" with "char*" then it will be like "char* s3,s4"
that is equal to char* s3;
char* s4;
So why are you saying that there is no "*" with s4, s4 didn't need any start as it's been used is keyword itself.
(1)
Bhimeswar said:
1 decade ago
Well done Sundar, you are rocking.
Gowthami said:
1 decade ago
Thanku very much Mr sundar.
Raja sekhar said:
1 decade ago
Welldone Answer Sundar
Vijeth said:
1 decade ago
';' in #define statement should be removed.
Irisi said:
8 years ago
Well done @Sundar.
Thanks.
Thanks.
Suuu said:
8 years ago
I can't understand. Please explain me.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers