Answer T4


pointer b pointing to a Bulb struct
#include <stdio.h>

struct Bulb
{
  int watts;
  int lumens;
};

/* function to print a Bulb */
void printBulb( struct Bulb *b )
{
  printf("watts = %d\tlumens = %d\n", b->watts, b->lumens );
}

int main()
{
  struct Bulb bulbA = {100, 1710 }, bulbB = {60, 1065};
  
  printBulb( &bulbA );        
  printBulb( &bulbB );
  
  return 0;
} 

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 *b )

The asterisk in struct Bulb* b 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.



Back to Puzzle Home