+ All Categories
Home > Documents > 'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a...

'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a...

Date post: 28-Dec-2015
Category:
Upload: dorcas-caldwell
View: 216 times
Download: 2 times
Share this document with a friend
Popular Tags:
21
'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures Use of structure pointers Structures and arrays Assigning structures
Transcript
Page 1: 'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.

'C' Programming With Structure Records

Purpose of structuresCoding a structure templateDefining a new data typeFunctions which communicate using structuresUse of structure pointersStructures and arraysAssigning structures

Page 2: 'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.

Purpose of structures

Computing applications often need data of different kinds to be associated. A typical place where this occurs is in a record. For example a database record for a particular student might contain the student number, full name, course-code and year of entry.

Page 3: 'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.

Record declaration

struct student { char studno[12]; char name[50]; char course[7]; int startyear;};

Page 4: 'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.

More examples

struct coord { float x,y; };struct complex { float real,imaginary; };

The data might have different types but does not have to be of different types. Structures also help with the naming of related parts of a record object.

Page 5: 'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.

Allocating memory to records

The above declarations don't create any space for data storage, but they provide a template for data of the appropriate types to be allocated. Let's allocate some storage to data variables:

struct coord origin, cursor; /* 2 x-y coordinates called origin and cursor */struct student sdn_year2[50]; /* array for 50 students */

Page 6: 'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.

Defining a new data type

Better to have one word e.g. COORD to declare the type of data than 2 words e.g. struct coord . This needs a typedef statement:

struct coord { float x,y; };typedef struct coord COORD; /* COORD now same as struct coord */COORD origin, cursor; /* declare 2 x-y coordinates called origin and cursor */

Page 7: 'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.

Use of the dot structure member access operator

Members x and y of origin and cursor can now be accessed as origin.x, origin.y, cursor.x and

cursor.y e.g.

origin.x=3.5;origin.y=2.0;

Page 8: 'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.

Having defined a new data type

We can declare stuctured data with a single word in a similar way that we use other built-in data types such as float and int. This simplification is useful as the name of the data type is going to appear in many places within our program, e.g. for declaring types of local data within functions or function parameters.

It may not seem the case, but having many smaller functions which communicate cleanly is going to make programming much, much easier.

Page 9: 'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.

Functions which communicate using structured parameters

float distance(COORD a, COORD b){ /* calculate distance between a and b */ float z,vert,horiz; /* z = distance, vert = y1 - y2, horiz = x1 - x2 */ horiz=a.x - b.x; /* the horizontal distance */ vert=a.y - b.y; /* the vertical distance */ /* calculate z as the hypotenuse of a right angle triangle */ z=sqrt((horiz*horiz) + (vert*vert)); /* Pythagorus theorem: */ return z; /* z*z = x*x + y*y */}

Page 10: 'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.

Functions which communicate using structured return values

COORD getcoord(void){ /* note returned type is COORD *//* prompts for and returns a coordinate */

COORD temp; printf("Enter x and y coordinates \n"); scanf("%f%f",&temp.x,&temp.y); return temp;}

Page 11: 'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.

A program using distance and getcoord part 1

#include <stdio.h>#include <math.h> /* needed to use sqrt() square root */ struct coord { float x,y; }; /* declare structure coord as having x and y members */typedef struct coord COORD; /* COORD is now same as struct coord */COORD origin, cursor; /* declare 2 x-y coordinates called origin and cursor */

/* function prototypes */float distance(COORD a, COORD b); COORD getcoord(void);

Page 12: 'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.

A program using distance and getcoord part 2

int main(void){ COORD origin, cursor; float separation; printf("enter details for origin:\n"); origin=getcoord(); printf("enter details for cursor:\n"); cursor=getcoord(); separation=distance(origin,cursor); printf("the distance between origin and cursor is %f\n",separation);

return 0;}

Page 13: 'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.

Use of structure pointers

Members x and y of coordinates a and b can also be accessed through pointers to a and b so that if pointer p stores the address of a: p=&a; then

p->x

directly accesses member x of a or a.x .

Page 14: 'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.

Rewrite the above program using pointers

/* declarations above here of headers , struct coord and typedef COORD same as before so not repeated */

float distance(COORD *a, COORD *b); /* a and b are now pointers to COORD */void getcoord(COORD *t); /* inputs coordinate through COORD* pointer t */

Page 15: 'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.

Rewrite using pointers slide 2

int main(void){ COORD origin, cursor, *orig,*curs; orig=&origin; curs=&cursor;

/* store addresses in pointers orig and curs */ float separation; printf("enter details for origin:\n"); getcoord(orig); printf("enter details for cursor:\n"); getcoord(curs); separation=distance(orig,curs); printf("the distance between origin and cursor %f\n" , separation); getch(); return 0;}

Page 16: 'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.

Rewrite using pointers slide 3

float distance(COORD *a, COORD *b){ /* calculate distance between a and b */

float z,vert,horiz; /* z = distance, vert = y1 - y2, horiz = x1 - x2 */ horiz=a->x - b->x; /* horizontal distance note -> pointer syntax */ vert=a->y - b->y; /* the vertical distance */ /* calculate z as the hypotenuese of a right angle triangle */ z=sqrt((horiz*horiz) + (vert*vert)); /* pythagorus theorem: */ return z; /* z*z = x*x + y*y */}

void getcoord(COORD *t){ /* inputs x-y coordinate using pointers */ printf("please enter x and y coordinates\n"); scanf("%f%f",&t->x,&t->y); /* -> has higher precedence than & */}

Page 17: 'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.

Structures and arrays

Having individual names for 10 coordinates is very awkward. We can declare the data as an array:

typedef struct coord { float x,y; } COORD;COORD graph[10];

We can then assign members of our structure array like this:

graph[9].x = 12.5; graph[9].y = 7.3; /* assign values to tenth element [9] of graph array */

Page 18: 'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.

Or we could input values into all coordinates using a loop

int i; for(i=0;i<10;i++){ printf("Enter x value for coordinate%d\n" ,i+1); scanf("%f",&graph[i].x); printf("Enter y value for coordinate%d\n" ,i+1); scanf("%f",&graph[i].y);}

Page 19: 'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.

Or using structure pointer notation

int i; for(i=0;i<10;i++){ printf("Enter x value for coordinate%d\n" ,i+1); scanf("%f",&(graph+i)->x); /* -> has higher precedence than & */ printf("Enter y value for coordinate%d\n" ,i+1); scanf("%f",&(graph+i)->y);}

Page 20: 'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.

A structure containing an array

typedef struct student { char name[30]; float mark;} STUDENT;

STUDENT you;printf ("enter name and mark\n");scanf ("%s%f",you.name,&you.mark);

/* you.name is the address of the name array *//* so no &ampersand needed to get address*//* BUT you.mark is not an array so needs & */

printf ("hello %s your mark is %f\n",you.name,you.mark);printf ("The first letter of your name is %c\n",you.name[0]);

Page 21: 'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.

Assigning structures

Unlike arrays, structures can be copied as a complete entity using a single assignment. This saves time. So instead of writing:

strcpy(temp.name,you.name); /* use function strcpy to copy a string */temp.mark = you.mark; /* can assign mark in one go */

we can write:

temp=you; /* copy structure in one go */


Recommended