#include <stdio.h> struct Bulb { int watts; int lumens; }; #define length 10 /* function to print a Bulb array */ void printArray( struct Bulb b[], int size ) { int j; for ( j=0; j<size; j++ ) printf("bulb %2d watts = %d\tlumens = %d\n", j, b[j].watts, b[j].lumens ); } int main(int argc, char *argv[]) { /* declare an array of 10 Bulbs */ struct Bulb lights[length] = { {100,1710}, {60,1065} }; printArray(lights, length); system("pause"); return 0; }
Comments:
The parameter b
in the function is actually the address
of the array.
The corresponding argument lights
in the function call is also
an address.
When the function is called, no copy is made of the array.
The syntax of arrays makes this easy to do, but obscures the fact that it is happening.
Remember that the name of an array (here, lights
) stands for its address,
so the call
printArray(lights, length);
passes the address of the array to printArray()
.
Inside the function, the expression
b[j].watts
accesses the array through its address, b
.