Lab problems

advertisement

Review

1.

State whether the following statements are true or false. a.

A struct type in C is a built-in data type b.

The tag name of a structure is optional c.

Structures maybe contain members of only one data type d.

A structure variable is used to declare a data type containing multiple fields. e.

It is legal to copy a content of a structure variable to another structure variable of the same type. f.

Structures are always passed to functions by pointers g.

Pointers can be used to access the members of structure variables. h.

We can perform mathematical operations on structure variables that contain only numeric type members. i.

The keyword typedef is used to define a new data type. j.

In accessing a member of a structure using a pointer p, the following two are equivalent: a.

(*p).member_name b.

p->member_name

2.

Given the declaration

struct abc a,b,c;

Which of the following statements are legal? a.

scanf (“%d”, &a); b.

printf(“%d”, b); c.

A = b; d.

A = b + c; e.

If (a > b)

Programming excises

1.

Define a structure data type called time_struct containing three members integer hour, integer minute and integer second. Develop a program that would assign values to the individual members and display the time in the following form:

16:40:51 add a function that is used to input values to the members and another function to display the time.

2.

Define a structure to represent a vector (a series of integers values) and write a modular program to perform the following tasks: a.

To create a vector b.

To modify the value of a given element c.

To multiply by scalar value d.

To display the vector in the form (10, 20, 30,…)

3.

Add three functions to problem 2 that accept two vectors as input parameters and a.

return the addition of two vectors. b.

return the subtraction of two vectors.

c.

return the vector product of two vectors.

4.

Write program for displaying the size of the data type in union and the size of the object of the union.

In this program, union of the set is declared and si is the object of the union set. The union set contains integer, character, float and long double data types.

The program shows the size of all the data types but it shows the size of the object 10 because the long double has the size 10

.

a.

Modify the program to print out address of each union’s memeber. b.

Change union to struct and print out address of each structure’s member.

5.

Study and implement the program bellow.

struct Person - The structure has 4 elements that describe a person.

function Person_create - The function is doing the following a.

Use malloc for "memory allocate" to ask the OS to give me a piece of raw memory. b.

Pass to malloc the sizeof( struct Person ) which calculates the total size of the struct, given all the fields inside it. c.

Use assert to make sure that I have a valid piece of memory back from malloc . The constant NULL is used to mean "unset or invalid pointer".

This assert is basically checking that malloc didn't return a NULL invalid pointer. d.

Initialize each field of struct Person using the x->y syntax, to say what part of the struct is initialized. e.

The function strdup is applied to duplicate the string for the name, just to make sure that this structure actually owns it. The strdup actually is like malloc and it also copies the original string into the memory it creates. function Person_destroy – The function is called to destroy Person structs i.e. free memory that was allocated to store structres. assert checks that the proper input is passed. The function free frees the memory that was allocated with malloc and strdup . If you don't do this you get a "memory leak".

function Person_print – Is used to print out people. It uses the same x->y syntax to get the field from the struct to print it.

function main - the main function call all previously defined functions and the struct Person to do the following: a.

Create two people, joe and frank . b.

Print them out, the %p format to print out where the program has actually put the struct in memory. c.

Age both of them by 20 years, with changes to their body too. d.

Print each one after aging them.

e.

Finally destroy the structures so we can clean up correctly.

This is what you should see after running the program.

6.

Convert the program above to not use pointers and malloc .

Download