#include <stdio.h>
struct Bulb
{
int watts;
int lumens;
};
/* 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 );
}
/* function to decrease light output */
void dimBulb( struct Bulb *b )
{
/* decrease output by 10 percent */
b->lumens = (b->lumens*90)/100;
}
int main()
{
int j;
/* declare and initialize a Bulb array */
#define length 5
struct Bulb lights[length] = { {100,1710}, {60,1065}, {100,1530}, {40,505}, {75,830} };
/* print the Bulbs */
printArray(lights, length);
printf("\n");
/* decrease light output of each Bulb */
for ( j=0; j<length; j++ )
dimBulb( &lights[j] );
/* print the Bulbs */
printArray(lights, length);
return 0;
}
Notice how dimBulb(&lights[j]) designates the address of an
array element.
Another way to do the same thing is:
dimBulb( lights+j );
Recall that the expression
array[j]
is equivalent to the expression
*(array + j )
The name of an array stands for the address of the cell at index 0.
The expression (array + j ) means "add the size of j
cells to the address array.
The address of each cell of the array is what we need to pass to the function.