![]() |
Site Archive (Complete) | |||
|
ABOUT US |
CONTACT |
ADVERTISE |
SUBSCRIBE |
SOURCE CODE |
CURRENT PRINT ISSUE |
NEWSLETTERS
|
RESOURCES
|
BLOGS
|
PODCASTS
|
CAREERS
|
||||
January 01, 2002
The New C:Variable Length Arrays, Part 3: Pointers and ParametersRandy 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.
Listing 1: Using pointers to arrays
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
|
|
||||||||||||||||||||||||||
|
|