How can I define a datatype with size constraint?

S

sandorf

When I write:
int a;
and I want another name for "int", I can simply write
typedef int interger

but what if I want a more descriptive name for the datatype
int a[2]; ?
typedef int[2] two_intergers of course won't work.

Thanks.
 
J

Jonathan Mcdougall

sandorf said:
When I write:
int a;
and I want another name for "int", I can simply write
typedef int interger

but what if I want a more descriptive name for the datatype
int a[2]; ?
typedef int[2] two_intergers of course won't work.

typedef int two_integers[2];


Jonathan
 
S

Sumit Rajan

sandorf said:
When I write:
int a;
and I want another name for "int", I can simply write
typedef int interger

but what if I want a more descriptive name for the datatype
int a[2]; ?
typedef int[2] two_intergers of course won't work.

typedef int two_ints[2];

and you can then use it like this:
two_ints two;

Regards,
Sumit.
 
V

Victor Bazarov

sandorf said:
When I write:
int a;
and I want another name for "int", I can simply write
typedef int interger

but what if I want a more descriptive name for the datatype
int a[2]; ?
typedef int[2] two_intergers of course won't work.

What book are you reading? I am surprised that [if you're reading
any book] it doesn't explain that you're supposed to do

typedef int two_integers[2];

V
 
M

Marcus Kwok

sandorf said:
When I write:
int a;
and I want another name for "int", I can simply write
typedef int interger

but what if I want a more descriptive name for the datatype
int a[2]; ?
typedef int[2] two_intergers of course won't work.

typedef int two_integers[2];

Also, note that they are "integers", not "intergers".
 
G

g....

sandorf said:
When I write:
int a;
and I want another name for "int", I can simply write
typedef int interger

but what if I want a more descriptive name for the datatype
int a[2]; ?
typedef int[2] two_intergers of course won't work.

Thanks.

typedef int two_intergers[2];

two_intergers var;
var[0] = 100;
var[1] = 200;

.....
 
M

Marcus Kwok

Sumit Rajan said:
typedef int two_ints[2];
and you can then use it like this:
two_ints two;

sandorf said:
oh, it works.
thanks

Please quote what you are following up to.

An easy way to remember how it goes, is to think about how you would
declare a variable of that type. For example, to declare an array of
two ints, it is:

int two_integers[2];

When you do a typedef, place the new name where you would normally place
the variable name, and put "typedef" at the beginning:

typedef int two_integers[2];

Remembering this can come in handy if you need to write complex typedefs
for function pointers, etc.
 
D

Default User

When I write:
int a;
and I want another name for "int", I can simply write
typedef int interger

but what if I want a more descriptive name for the datatype
int a[2]; ?
typedef int[2] two_intergers of course won't work.

Thanks.

typedef int two_intergers[2];

two_intergers var;
var[0] = 100;
var[1] = 200;


This sort of usage (I know it's what the OP asked for) is considered by
many to be very bad. It obfuscates the code for no particular advantage.

About the only typedef that's useful is that of the function pointer,
as those declarations are somewhat non-intuitive and can be easy to
screw up.


Brian
 
G

Greg Comeau

When I write:
int a;
and I want another name for "int", I can simply write
typedef int interger

but what if I want a more descriptive name for the datatype
int a[2]; ?
typedef int[2] two_intergers of course won't work.

A typedef usually works like a declaration.
Get one of them:

int a[21];

and then put typedef in front of it all:

typedef int a[21];
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top