confusion in pointer declarations

what is the difference between these two
char* val;
char *val;

and also
typedef char *cpointer;

Now cpointer is pointer to a character

typedef [attributes] DataType AliasName;

but according to typedef

*cpointer(alias) should give me character right
Last edited on
what is the difference between these two
char* val;
char *val;
None. Whitespace is ignored.

typedef char *cpointer;

Now cpointer is pointer to a character

typedef [attributes] DataType AliasName;

but according to typedef

*cpointer(alias) should give me character right
I don't quite understand what you're asking so my answer may not make sense:
1
2
3
4
5
typedef char * pchar;
int i=0;
*(pchar)&i;
//is the same as
*(char *)&i;
typedef [attributes] DataType AliasName;

you mean to say

typedef char* pchar
(char*) is considered a datatype
pchar is the aliasname

and also

typedef char *pchar
char* is considered as datatype
pchar is the aliasname

why is this inconsistency?why doesn't the compiler throw an error?why is this intelligence there in the compilation

typedef char * pchar;
why * is not considered as alias name as per the typedef syntax above and throw an error.

As you explained


char *val;
char* val;
has no difference

char* val,val1,val2
are all these char pointers

char *val,val1;

in this is val is a pointer and val1 is a character right??
typedef char *pchar
char* is considered as datatype
pchar is the aliasname

why is this inconsistency?why doesn't the compiler throw an error?why is this intelligence there in the compilation
It's not inconsistent. A type followed by any number of *s is considered a pointer, therefore the typedef is declaring pchar to be an alias for 'char *'. I don't see how you can call this inconsistent.

typedef char * pchar;
why * is not considered as alias name as per the typedef syntax above and throw an error.
One of the reasons was stated in my previous paragraph. The other is that it wouldn't make sense to declare * as a type.

char *val;
char* val;
has no difference

char* val,val1,val2
are all these char pointers

char *val,val1;
Whitespace is ignored, therefore:
char *a;
is the same as
char* a;
and
char * a;
Therefore,
char *a,b;
is the same as
char* a,b;
Which is the same as
char *a;
char b; //(Note: I do admit that, in the above declaration, making b a non-pointer is somewhat inconsistent.)

So the rule is that a type can be:
A. A built-in type.
B. A user-defined type.
C. A, B, or C, followed by a *.
D. A, B, or C, follow by a &.
Last edited on
Topic archived. No new replies allowed.