Typedef of a fixed length array

J

Jens Thoms Toerring

Is there a way to define a typedef to be an array of fixed length?

Did you consider

typedef int array_of_10_ints[ 10 ];

int main( ) {
array_of_10_ints x;
size_t i;
for ( i = 0; i < sizeof x / sizeof *x; i++ )
x[ i ] = i;
....

The real challenge would be to typedef an array of variable
length, I guess;-)
Regards, Jens
 
J

James Kuyper

Is there a way to define a typedef to be an array of fixed length?

Did you consider

typedef int array_of_10_ints[ 10 ];

int main( ) {
array_of_10_ints x;
size_t i;
for ( i = 0; i < sizeof x / sizeof *x; i++ )
x[ i ] = i;
...

The real challenge would be to typedef an array of variable
length, I guess;-)

It's not much of a challenge, but the typedef can only have block scope
(6.7.7p2).

(6.7.7p8):
EXAMPLE 5 If a typedef name denotes a variable length array type, the
length of the array is fixed at the time the typedef name is defined,
not each time it is used:
void copyt(int n)
{
typedef int B[n]; // B is n ints, n evaluated now
n += 1;
B a; // ais n ints, n without += 1
int b[n]; // a and b are different sizes
for (int i = 1; i < n; i++)
a[i-1] = b;
}
 

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,952
Messages
2,570,111
Members
46,691
Latest member
Wilhemina5

Latest Threads

Top