Hi all,
What is the difference between char *a and char * a?
Where char * a can be used?
Thank you in advance.
Printable View
Hi all,
What is the difference between char *a and char * a?
Where char * a can be used?
Thank you in advance.
The difference is that char *a has one space less than char * a. Read Stroustrup's answer to the FAQ [url=http://www2.research.att.com/~bs/bs_faq2.html#whitespace]Is ``int* p;'' right or is ``int *p;'' right?[/quote] (C++ FAQ, but applicable to C)Quote:
Originally Posted by sarathius
No difference in there declaration. Just one of C's many faults.
Why is ignoring whitespace a fault?
The language is specific. The * ties to whatever variable it is next to, not to whatever type it is next to.
There is no more confusion there then there would be if you had the language specify it tied to the type instead of the variable. In fact, if it did tie to the type, then this line would be impossible to declare:It would be impossible to declare a pointer and a non-pointer on the same line if we had the language work as "specific" as you suggest.Code:type* aptr, notaptr;
Quzah.
Well, since this is C99 now, you couldn't do:Or whatever other random situation it would apply. It would be a problem now, because people are used to doing it one way, and it would be a drastic change. It wouldn't have been an issue when the language was designed, although honestly I don't see your way being any better.Code:for( int x = 0, *p = &x; ... )
It's not any more difficult to tell someone "it applies to the variable it is by" than it is to say "it applies to the type it is next to". In fact, if we did it your way, this would generate an error:It would have to, because it wouldn't have a type to bind to if we did it your way, and making it bind to anything it is by would be even more confusing than one or the other.Code:type *ok, *error;
"Why does this only give one pointer?"Code:type *x, y;
"Because it binds to the variable not the type."
"Why does that give two pointers?"Code:type* taterptr1, taterptr2;
"Because it binds to the type."
"Why does that give two pointers?"Code:type *taterptr1, *taterptr2;
"Because it binds to the type ... or the variable?"
It would cause more confusion than it would clear up.
Quzah.
Wow, you shure do type fast...
Yes, yes I do.
Quzah.