Puzzle T4


Call by Vaue (using an address)

pointer b pointing to a Bulb struct

Modify the answer to Puzzle 3 so that the parameter to the printBulb() function is the address of the Bulb it is to print.

#include <stdio.h>

/* declaration of type Bulb */
struct Bulb
{
  int watts;
  int lumens;
};

/* function to print a Bulb */
void printBulb( struct Bulb *b )
{
}

int main()
{
  /* declare and initialize two Bulbs */

  /* print values of both Bulbs */
  printBulb( &bulbA );
  printBulb( &bulbB );
	
  return 0;
} 

Hint: If you have a pointer to a struct, say ptr then the members of the struct can be accessed by (*prt).member where (*prt) follows (dereferences) the pointer to the struct, and then the usual dot notation, .member accesses the member. This combination is so common that there is a shorthand notation for it:

ptr->member       is the same as       (*ptr).member

For example, if blb is a pointer to a struct Bulb, then b->watts and b->lumens accesses its members.



Previous Page        Answer         Next Page         Home