#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*..??

Recommended Answers

All 5 Replies

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!

commented: Nice +14

thanks everyone... query solved!!

In which case: Mark as solved? ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.