By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 1,005 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

a question about typedef syntax

P: n/a
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

Jun 10 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Jess wrote:
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
Jun 10 '07 #2

P: n/a
On 2007-06-10, Jess <wd***@hotmail.comwrote:
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];
Jun 10 '07 #3

P: n/a
On Jun 11, 12:25 am, Szabolcs <szhor...@gmail.comwrote:
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.
Jun 10 '07 #4

P: n/a
On Jun 11, 12:28 am, Old Wolf <oldw...@inspire.net.nzwrote:
On Jun 11, 12:25 am, Szabolcs <szhor...@gmail.comwrote:
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.
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 11 '07 #5

P: n/a

James Kanze <ja*********@gmail.comwrote in message ...
const int* ptr ;
Then a newbie gets the idea to make the pointer const:

const int const* ptr:

Ouch!
int const* ptr;
Once you force your habits to that:

int const * const ptr:

Aahhhh.

<G>

--
Bob R
POVrookie
Jun 11 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.