Re: static typing (Learning Cocoa Chapter 13)
Re: static typing (Learning Cocoa Chapter 13)
- Subject: Re: static typing (Learning Cocoa Chapter 13)
- From: Dennis De Mars <email@hidden>
- Date: Fri, 21 Sep 2001 15:14:56 -0700
On Friday, September 21, 2001, at 02:09 PM, Erik M. Buck wrote:
In the name of aesthetic flame wars:
The asterisk indicates that a variable is a pointer not that a type is a
pointer. The asterisk therefore belongs with the variable and not the
type.
The simplest highlight of that fact follows:
char *foo, bar, *baz;
foo is a pointer (probably 4 bytes)
bar is a character (probably 1 byte)
baz is a pointer (probably 4 bytes)
If the pointerness was part of the type then
char* foo, bar, *baz;
would make foo a pointer, bar a pointer, and baz a pointer to a pointer
which is of course wrong and ludicrous.
Right. The general rule in C is that the keyword at the beginning is
the type of all the entities that follow, and if the variable is
something like a pointer or a function, you declare the variable by
applying the operators that will yield the type specified at the
beginning of the statement. So, you can declare a pointer to char and a
function returning char as:
char *foo, bar();
So you can write "char*" and think of it as a type as long as you are
declaring only one variable, but thinking of it this way will trip you
up if you declare several variables.
There is a way to actually get C to treat "char*" as a pointer type, and
that is with a typedef:
typedef char *charPtr;
Then
charPtr a, b, c;
will be equivalent to:
char *a, *b, *c;
This is the only way to get C to use "char*" as a type, it can't be done
without going through a typedef, which could be considered a syntactic
idiosyncrasy in C.
- Dennis D.