FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
C++
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
January 01, 2002
The New C:

Variable Length Arrays, Part 3: Pointers and Parameters

(Page 1 of 7)
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

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 —
1 | 2 | 3 | 4 | 5 | 6 | 7 Next Page
RELATED ARTICLES
No Related Articles
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK