Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #7301 > unrolled thread
| Started by | "raphfrk@gmail.com" <raphfrk@gmail.com> |
|---|---|
| First post | 2011-06-30 08:41 -0700 |
| Last post | 2011-07-01 06:50 -0400 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.c
Typedef of a fixed length array "raphfrk@gmail.com" <raphfrk@gmail.com> - 2011-06-30 08:41 -0700
Re: Typedef of a fixed length array jt@toerring.de (Jens Thoms Toerring) - 2011-06-30 16:15 +0000
Re: Typedef of a fixed length array James Kuyper <jameskuyper@verizon.net> - 2011-07-01 06:50 -0400
| From | "raphfrk@gmail.com" <raphfrk@gmail.com> |
|---|---|
| Date | 2011-06-30 08:41 -0700 |
| Subject | Typedef of a fixed length array |
| Message-ID | <6657b661-4ea7-47d3-94c4-28e415060a29@34g2000pru.googlegroups.com> |
Is there a way to define a typedef to be an array of fixed length?
[toc] | [next] | [standalone]
| From | jt@toerring.de (Jens Thoms Toerring) |
|---|---|
| Date | 2011-06-30 16:15 +0000 |
| Message-ID | <973lorF328U1@mid.uni-berlin.de> |
| In reply to | #7301 |
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://toerring.de
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2011-07-01 06:50 -0400 |
| Message-ID | <iuk8q9$dl0$1@dont-email.me> |
| In reply to | #7304 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web