POINTER WORKSHEET-1

The main program keeps track of information on members of a health club. It calls a function AddMember() when there is a new membership. The membership counts are kept track of in main(), so main() could pass this info to other functions.

 

void main()
{
int	total_cnt,	At memory location 1000
	family_cnt,	At memory location 2000
	adult_cnt,	At memory location 1600
	child_cnt;	At memory location 2200

The variables above are known to main(), not AddMember(). AddMember() will not recognize their names.

The function header for AddMember is

void AddMember(int *totalptr, int *fptr, int *aptr, int *cptr)

where totalptr is a pointer to the total count (total_cnt), fptr is a pointer to the family count (family_cnt), etc.

1. Write a calling statement for AddMember(), from main.
AddMember(&total_cnt, &family_cnt, &adult_cnt, &child_cnt);

2. Assume the variables have these current values, total_cnt = 204, family_cnt = 52, adult_cnt = 108, child_cnt = 96, when main() calls AddMember(). In the chart below, write the value of the expression, or check the ERROR column if the expression would result in a syntax error.

				VALUE		ERROR
If the expresion is IN main
	total_cnt		204       	_____	
	&total_cnt		1000		_____
	*totalptr		__________	__X__
	&totalptr		__________	__X__
Main does not recognize totalptr.
	adult_cnt		108		_____
	&adult_cnt		1600		_____
	&child_cnt		2200      	_____
	*family_cnt		__________	__X__
Variable family_cnt is not a pointer, so you can not dereference it.
If the expression is IN AddMember
	totalptr		1000		_____
	fptr			2000		_____
	*aptr			108		_____
	&cptr			***		_____
	&total_cnt		__________	__X__
	*total_cnt		__________	__X__
AddMember does not know what total_cnt is.
	*totalptr		204		_____
	*fptr			52		_____

*** Since cptr is a variable, its value is stored somewhere, so IT DOES have an address, you just don't know what it is. Just using &cptr will not get an error, because THERE IS an address, but it probably is not what you intended. Writing something like &cptr = 20 WILL get an error.

NOTE: &total_cnt = 1000 = totalptr. So we usually have &variablename in the calling statement when the argument is a pointer data type. (NOT *variablename, even though the * is in the function header!!)

adult_cnt = 108 = *aptr. So we usually use *pointername in the code of the function when the argument is a pointer data type.