C Programming

Practise Exercise 11a: Pointers & Structures

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()


	item.name = "Red Plum Jam"
	item.id = 0
	item.price = 0.0

2. After return from editrecord()


	item.name = "Baked Beans"
	item.id = 220
	item.price = 2.20

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


	item.name = "Baked Beans"
	item.id = 220
	item.price = 2.75


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