James Kuyper
7/1/2011 10:51:00 AM
On 06/30/2011 12:15 PM, Jens Thoms Toerring wrote:
> raphfrk@gmail.com <raphfrk@gmail.com> wrote:
>> 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[i];
}
--
James Kuyper