Structures and Arrays, Part I

Structures in C have their own issues when it comes to structure-pointers and structures within pointers. I wrote about this issue a few months back.

Another structure issue occurs with arrays within structures and arrays of structures. It also involves interesting notation — especially when you add pointers to the mix.

Like any variable, you can stock a structure with one or more arrays:

struct player {
    char name[32];
    int scores[10];
};

The arrays are accessed like any member in the structure: use the dot notation or reference a member or use the pointer -> notation when the structure is a pointer.

For example, if one were a player structure, you use one.scores[1] to access the second element in the scores array. If one were a pointer to a player structure, you use one->scores[1]. But first things first.

The following code allocates one as a player structure, fills and outputs the values:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

int main()
{
    struct player {
        char name[32];
        int scores[10];
    } one;
    int x;

    /* assign player name */
    strcpy(one.name,"Billy Zlotnick");

    /* create random scores */
    srand( (unsigned)time(NULL) );
    for( x=0; x<10; x++)
        one.scores[x] = rand() % 100;

    /* output results */
    printf("%s's scores:\n",one.name);
    for( x=0; x<10; x++)
        printf(" %3d",one.scores[x]);
    putchar('\n');

    return(0);
}

The player structure is defined starting at Line 8 and variable one is declared at Line 11.

The structure’s name member is filled at Line 15. The scores member is filled with random values at Lines 19 and 20.

Because member name is a char array (string), the strcpy() function accesses it directly, one.name.

Each of the int values stored in the scores array are accessed with structure dot and array bracket notation: one.scores[x].

Results are output from Lines 23 through 26:

Billy Zlotnick's scores:
75   2  34  79  57  60  33  57  87  86

To add a level of complexity, suppose the player structure were itself an array:

struct player {
    char name[32];
    int scores[10];
} p[3];

Above, three structures of the player type are declared. The following update to the code fills each element with data:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

int main()
{
    struct player {
        char name[32];
        int scores[10];
    } p[3];
    int x,y;

    /* assign player names */
    strcpy(p[0].name,"Billy Zlotnick");
    strcpy(p[1].name,"Franny Blortz");
    strcpy(p[2].name,"Oscar Papadapolous");

    /* create random scores */
    srand( (unsigned)time(NULL) );
    for( y=0; y<3; y++)
    {
        for( x=0; x<10; x++)
            p[y].scores[x] = rand() % 100;
    }

    /* output results */
    for( y=0; y<3; y++)
    {
        printf("%s's scores:\n",p[y].name);
        for( x=0; x<10; x++)
            printf(" %3d",p[y].scores[x]);
        putchar('\n');
    }

    return(0);
}

Each of the name members are assigned strings at Lines 15 through 17. The format p[n].name is used, where the brackets represent structures within the array.

Where the notation gets interesting is at Lines 24 and 32: The format p[y].scores[x] references a structure within the array and an element of the scores member. It looks complex, but if you reduce each item individually, the notation is familiar: p[y] is one of the structure array elements and name[x] is an element within the given structure.

Here’s sample output:

Billy Zlotnick's scores:
  34  32  93  52  65  19  65  71  66  98
Franny Blortz's scores:
  80  18  67  17  13  14  15  82  95  55
Oscar Papadapolous's scores:
  22   4  66  12  40  51  17  32  92  25

 

In next week’s Lesson, I toss pointers into the mix.

Leave a Reply