C Programming

Practise Exercise 11: Pointers & Structures

1. Declare a pointer to a structure of type date called dates.

struct dates dates;
struct *date *dates;
struct dates date;
struct date *dates;

2. If the above structure of type date comprises three integer fields, day, month, year, assign the value 10 to the field day using the dates pointer.

dates.day = 10;
dates->day = 10;
dates = 10.day;
day.dates = 10;

3. A structure of type machine contains two fields, an integer called name, and a char pointer called memory. Show what the definition of the structure looks like.

Choice 1

	struct machine {
		int name;
		char memory;
	}
Choice 2

	machine {
		name : integer;
		memory : char^;
	};
Choice 3

	struct machine {
		int name;
		char *memory;
	};

4. A pointer called mpu641 of type machine is declared. What is the command to assign the value NULL to the field memory.

mpu641->memory = (char *) NULL;
mpu641.memory = 0;
mpu641-memory = 0;
strcpy( mpu641.memory, NULL);

5. Assign the address of the character array CPUtype to the field memory using the pointer mpu641.

mpu641.memory = &CPUtype;
mpu641->memory = CPUtype;
strcpy( mpu641.memory, CPUtype);
mpu641.memory = CPUtype;

6. Assign the value 10 to the field name using the pointer mpu641.

mpu641.name = 10;
mpu641->name = 10;
mpu641.name = *10;
*mpu641.name = 10;

7. A structure pointer times of type time (which has three fields, all pointers to integers, day, month and year respectively) is declared. Using the pointer times, update the field day to 10.

times.day = 10;
*(times->day) = 10;
*times.day = 10;
times.day = *10;

8. An array of pointers (10 elements) of type time (as detailed above in 7.), called sample is declared. Update the field month of the third array element to 12.

*(sample[2]->month) = 12;
sample[3].month = 12;
*sample[2]->month = 12;
*(sample[3]->month) = 12;


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