#include <stdio.h> struct Bulb { int watts; int lumens; }; /* function to print a Bulb */ void printBulb( struct Bulb *blb ) { printf("watts = %d\tlumens = %d\n", blb->watts, blb->lumens ); } int main(int argc, char *argv[]) { struct Bulb bulbA = {100, 1710 }, bulbB = {60, 1065}; printBulb( &bulbA ); printBulb( &bulbB ); system("PAUSE"); return 0; }
Comments: Functions in ANSI C always use call by value for parameter passing. However, the value that is passed can be an address. The function call
printBulb( &bulbA );
passes a parameter which is the address of bulbA
.
The ampersand in &bulbA
means "address of".
This is just a single value -- a 32-bit address.
The function declaration shows that the function expects such an address:
void printBulb( struct Bulb *blb )
The asterisk in struct Bulb *blb
means "follow an address".
A struct can be initialized in an initializer list:
struct Bulb bulbA = {100,1710};
The values in the list are assigned to the members of the struct, in order.