[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Typedef of a fixed length array

raphfrk@gmail.com

6/30/2011 3:41:00 PM

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

jt

6/30/2011 4:15:00 PM

0

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;-)
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://t...

James Kuyper

7/1/2011 10:51:00 AM

0

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