January 01, 2002
The New C:
Variable Length Arrays, Part 3: Pointers and Parameters
Randy Meyers
Randy shows that pointers to VLAs are as convenient as pointers to normal arrays. The question is: how many programmers understand pointers to arrays in the first place? Look here to see if you're one of them.
January 2002/The New C/Listing 1
void ex1()
{
int i;
int a[3];
int (*pa)[3] = &a;
for (i = 0; i < 3; ++i)
(*pa)[i] = 1;
// Save the result of calling f()
// so the bounds of vla, pvla, and
// the loop will be consistent even
// if f() returns a different value
// each time it is called
int bounds = f();
int vla[bounds];
int (*pvla)[bounds] = &vla;
for (i = 0; i < bounds; ++i)
(*pvla)[i] = 1;
}
End of Listing
1
|
2
|
3
|
4
|
5
|
6
|
7
Next Page