C Programming

Practise Exercise 11a: Pointers & Structures
This program introduces a structure which is passed to a function editrecord() as a reference and accessed via a pointer goods.

Determine the output of the following program.


#include <stdio.h>
#include <string.h>

struct  record {
	char name[20];
	int id;
	float price;
};

void editrecord( struct record * );

void editrecord( struct record *goods )
{
	strcpy( goods->name, "Baked Beans" );
	goods->id = 220;
	(*goods).price = 2.20;
	printf("Name = %s\n", goods->name );
	printf("ID = %d\n", goods->id);
	printf("Price = %.2f\n", goods->price );
}

main()
{
	struct record item;

	strcpy( item.name, "Red Plum Jam");
	editrecord( &item );
	item.price = 2.75;
	printf("Name = %s\n", item.name );
	printf("ID = %d\n", item.id);
	printf("Price = %.2f\n", item.price );
}

1. Before call to editrecord()

2. After return from editrecord()

3. The final values of values, item.name, item.id, item.price

Answer


ęCopyright B Brown. 1984-1999. All rights reserved.