a question about typedef syntax

J

Jess

Hello,

I think the syntax of typedef is

typedef existing-type new-type

However, I've seen a statement

typedef string stringarray[42];
From the programs that use stringarray, I can deduce it represents the
type string[42]. If so, why is it not defined as

typedef string[42] stringarray?

Thanks,
Jess
 
S

Szabolcs

Jess said:
Hello,

I think the syntax of typedef is

typedef existing-type new-type

However, I've seen a statement

typedef string stringarray[42];
From the programs that use stringarray, I can deduce it represents the
type string[42]. If so, why is it not defined as

typedef string[42] stringarray?

Thanks,
Jess

The syntax for typedef is the same as the syntax for declaring
variables, but the name of the new type (new-type in your example) is
substituted for the variable name.

Szabolcs
 
C

Charles Bailey

Hello,

I think the syntax of typedef is

typedef existing-type new-type

Not quite, the typedef syntax is (informally) if "X;" would be a valid
declaration of a variable A, then "typedef X;" defines A as a synonym
for the type that A would have had in the corresponding variable
declaration. This reduces to your form when "existing-type" is
something simple like "int".
However, I've seen a statement

typedef string stringarray[42];
From the programs that use stringarray, I can deduce it represents the
type string[42]. If so, why is it not defined as

typedef string[42] stringarray?

To declare A as an array of 42 string you write:
string A[42];
and not
string[42] A;

so the corresponding typedef would be:
typedef string stringarray[42];
 
O

Old Wolf

The syntax for typedef is the same as the syntax for declaring
variables, but the name of the new type (new-type in your example)
is substituted for the variable name.

And the keyword 'typedef' is added as a qualifier would be.
 
J

James Kanze

And the keyword 'typedef' is added as a qualifier would be.

As a storage class would be. Although order is irrelevant
according to the standard, and things like:
int typedef *ptr ;
and
const int* ptr ;
are technically legal, good programming practice always puts the
typedef first, and the best current practice tends to put the
const after the int, i.e.:
typedef int* ptr ;
and
int const* ptr ;

For historical reasons, actual practice concerning the placement
of constvaries a lot. On the other hand, the C standard says
"The placement of a storage-class specifier other than at the
beginning of the declaration specifiers in a declaration is an
obsolescent feature", and C considers typedef a storage-class
specifier. (I have no idea whether there is any intention to
ever adopt this rule in C++. But for as long as I can remember,
by convention, the various non-type specifiers (register,
static, extern, mutable, auto, inline, virtual, explicit,
typedef, and friend) have always come before any type specifier.
It's a convention that shouldn't be ignored.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top