Re: If you could change the C or C++ or Java syntax, what would you like different?
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Mon, 18 Oct 2010 08:12:42 -0700
felix@xxxxxxxxxxxx (Felix Palmen) writes:
* Keith Thompson <kst-u@xxxxxxx>:
felix@xxxxxxxxxxxx (Felix Palmen) writes:
| typedef struct {
| float speed;
| char *model;
|
| void (*accelerate)(void *this, float delta);
| void (*decelerate)(void *this, float delta);
| } Car;
Would you really call "Car" an alias?
(finally ... a car example :D)
Yes, the name "Car" (created by the typedef) is nothing more than an
alias for the type "struct { ... }".
No.
Yes.
You cannot sensibly argument that a notation of "struct { ... }"
identifies a type -- it describes it. The description may be
unambiguous, so it would be possible to describe the type each and every
time you need it, but for /defining/ a type, it should get its own
identifier -- just like defining a function, a variable, etc.
Not all C types have simple names; in fact most of them don't. We have
int, short, and char, but we also have long double, unsigned long long
int, char*, int[42], and struct { int x; double y; }.
To take a simpler example:
typedef int arr[10];
This declaration makes "arr" an alias for the existing type
"int[10]", a type that otherwise wouldn't have a name of its own
other than "int[10]". Consider:
typedef int word;
typedef int arr[10];
typedef struct { /* ... */ } Car;
All three of these create an alias for an existing type. The only
difference is complexity and the fact that, in at least the third
case, the type is created by the declaration (and would have been
created even without the "typedef" keyword and the "Car" identifier).
struct car {
/* ... */
};
typedef struct car Car;
Another way to do it, this way you define a type called "struct car" --
one of the peculiarities of C I don't like. You need to use typedef if
you want to define your type in the "global" identifier namespace. But
at least, that's the reason typedef is perfectly sensible named, IMO.
typedef is the *only* way that a type can have a name that's a single
identifier (as opposed to a keyword).
A digression: Many programmers (including me) prefer *not* to use
typedefs for structs. I'd just declare
struct car {
/* ... */
};
and then refer to the type as "struct car". The type already has
a perfectly good name; it doesn't need another one.
But if you dislike having to repeat the "struct" keyword every time you
refer to the type, you can use a typedef, and even omit the tag if you
don't need to refer to the type name inside its declaration. Keep in
mind that you can use the same identifier for the tag and the typedef.
End of digression.
Your original example doesn't give a name to the created type,
but that's the only difference.
It does, the name is "Car" (as opposed to "struct car"). Of course, if
you give it both names, one is obviously an alias for the other.
And if you don't give it both names, then "Car" is an alias for
an anonymous struct type.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.
- Follow-Ups:
- References:
- Re: If you could change the C or C++ or Java syntax, what would you like different?
- From: Nick Keighley
- Re: If you could change the C or C++ or Java syntax, what would you like different?
- From: Jon
- Re: If you could change the C or C++ or Java syntax, what would you like different?
- From: Keith Thompson
- Re: If you could change the C or C++ or Java syntax, what would you like different?
- From: Jon
- Re: If you could change the C or C++ or Java syntax, what would you like different?
- From: Felix Palmen
- Re: If you could change the C or C++ or Java syntax, what would you like different?
- From: Keith Thompson
- Re: If you could change the C or C++ or Java syntax, what would you like different?
- From: Felix Palmen
- Re: If you could change the C or C++ or Java syntax, what would you like different?
- From: Keith Thompson
- Re: If you could change the C or C++ or Java syntax, what would you like different?
- From: Felix Palmen
- Re: If you could change the C or C++ or Java syntax, what would you like different?
- From: Keith Thompson
- Re: If you could change the C or C++ or Java syntax, what would you like different?
- From: Felix Palmen
- Re: If you could change the C or C++ or Java syntax, what would you like different?
- Prev by Date: Re: PRIME1 (SPOJ)
- Next by Date: Re: PRIME1 (SPOJ)
- Previous by thread: Re: If you could change the C or C++ or Java syntax, what would you like different?
- Next by thread: Re: If you could change the C or C++ or Java syntax, what would you like different?
- Index(es):
Relevant Pages
|