#include<stdio.h>
#define CH char*;
int main(){
CH a,b;
printf("%d %d\n",sizeof(a),sizeof(b));
return 0;
}
here the o/p is 4 1. why isnt b also of type char*..??
#include<stdio.h>
#define CH char*;
int main(){
CH a,b;
printf("%d %d\n",sizeof(a),sizeof(b));
return 0;
}
here the o/p is 4 1. why isnt b also of type char*..??
A few clarifications and safe practices that add to the answers from , and .
The core reason: the * in a declaration is part of the declarator, not the base type. In a comma-separated declaration the * applies only to the name it is attached to. That is why one identifier ends up a pointer and the other a plain char. To avoid the trap, declare pointers one-per-declaration or make the pointer-ness explicit.
Macros are plain text substitution, so using a #define to "make a type" does not change how the compiler parses the declarators. If you accidentally include a trailing semicolon in the macro you will also introduce syntax errors because the preprocessor just pastes text. typedef genuinely creates a new type name, which is why ’s typedef approach makes both names pointers — but note typedefs can hide pointer-ness, so choose clear names (for example include _ptr or _t in the typedef name if you use them).
Two practical tips you can apply immediately:
sizeof returns a size_t. Print it portably with %zu (C99+) or cast for older compilers:
printf("%zu %zu\n", sizeof(a), sizeof(b)); Using the wrong printf specifier is undefined behavior even if it sometimes "looks right."
sizeof(pointer) gives the pointer width (commonly 4 on 32-bit, 8 on 64-bit). sizeof(char) is always 1 by the standard. If you want the size of the object a pointer points to, use sizeof *ptr rather than sizeof ptr.
Summary: prefer one declaration per variable, avoid macros for type aliases, use typedef when a true type alias is desired, and print sizeof with the correct format.
Jump to Post— Moschops 683Because that's how C works.
This:
char* a,b;creates two variables. a is a char*, b is a char. They are NOT both pointers. If you want them both to be pointers:
char *a, *b;
Jump to Post— JasonHippy 739Although, if you use a typedef instead of a macro your original code would work:
#include<stdio.h> typedef char* CH; int main() { CH a,b; printf("%d %d\n",sizeof(a),sizeof(b)); return 0; }Now we've defined a new type called CH, which is a char*, so now a and b …
Because that's how C works.
This:
char* a,b;
creates two variables. a is a char*, b is a char. They are NOT both pointers. If you want them both to be pointers:
char *a, *b;
A good example of why you should not declare multiple variables together, but better one per line / declaration statement.
Although, if you use a typedef instead of a macro your original code would work:
#include<stdio.h>
typedef char* CH;
int main()
{
CH a,b;
printf("%d %d\n",sizeof(a),sizeof(b));
return 0;
}
Now we've defined a new type called CH, which is a char*, so now a and b are both of type char*.
Which might be what you were originally aiming for!
Personally, I try to avoid using typedefs as they tend to obfuscate code, making it harder to understand what is going on. But there are certain situations when you might want to use them!
thanks everyone... query solved!!
In which case: Mark as solved? ;)
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.