473,513 Members | 2,275 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a question about typedef syntax

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
5 3487
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
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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
17160
by: Richard van Wegen | last post by:
Dear All I'm hoping someone can show me the correct way to typedef a template class - i.e. something along the lines of typedef boost::shared_ptr<T> shared_ptr_multithread<T>; (syntax is...
5
2990
by: Roger Leigh | last post by:
Although I've got over most of my template-related problems, I'm having trouble when I started to use default template parameters. For template type T, I've typedef'd this as object_type and then...
8
4673
by: Fred L. Kleinschmidt | last post by:
I need to know the largets value representable in a variable. However, I do not know the variable's true type - only that it is some kind of int. It may be any of the following: #typedef Newtype...
5
41020
by: Cancerbero | last post by:
Hi (first, excuse me for my bad english) As I know, the semantics for typedef is: typedef A B; I think this makes B a synonym of A, where A is an existing data type. Is that right? Based...
2
2184
by: Patrick Kowalzick | last post by:
Hello NG, sorry to bother again, but I am a lit surprised that I got no answer on my post (attached below). So I refined the code a little bit :-). If there is a typedefed class X inside a...
20
2073
by: ma0001yu | last post by:
Hi, all. I feel confuse about below struct definition: typedef struct TAG { comments.... }; What my confusion is: is typedef extra??why we not just use
4
1418
by: vcinquini | last post by:
I've always learned about typedef as a statement that creates an alias for a type. For example: typedef long DWORD; but I'm confused about the following typedef. Which is the type and which...
8
28431
by: Mohammad Omer Nasir | last post by:
Hi, i made a structure in header file "commonstructs.h" is: typedef struct A { int i; A( ) {
2
1462
by: linq936 | last post by:
Hi, I have a typedef like this, typedef (char* ) arrp; I want to define a type which is an array, the array has 20 elements each of which is a char*. I get compile error:
0
7265
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
7114
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7541
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5098
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4751
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1607
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
461
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.