Download presentation
Presentation is loading. Please wait.
1
Introduction to C Programming CE00312-1 Lecture 10 Data Structures typedef and struct
2
Struct Group several pieces of related information together and is a collection of variables, referred to as fields, under a single name. Fields can be of differing types and must be uniquely named within the structure As the storage needed cannot be known in advance by the compiler, a definition is first required. A structure can be defined as a new named type thus extending the number of available types. It can use other structures, arrays or pointers as some of its members, though this can get complicated unless you are careful. Uses the keyword struct
3
Defining a struct A structure type is usually defined near to the start of a file using a typedef statement that defines and names a new type allowing its use throughout the program. Typedef statements usually occur just after the #define and #include statements in a file. Here is an example structure definition. typedef struct { char name[64], course[128]; int age, year; }student; This defines a new type student. Variables of type student can then be declared as follows. student st_record;
4
Variations in declaring structures struct date { int month, day, year; } todaysdate, purchase_date; struct date { int month, day, year; } todaysdate = { 25,9,1985 }; struct date { int month, day, year ; } dates[100];
5
Declaring structures in this way, however, prevents later use of the structure definition in the program since the structure definition is bound to the variable name that follows the right brace of the structure definition.
6
Structures containing structures Structures can also contain structures. struct date { int month, day, year; }; struct time { int hours, mins, secs; }; struct date_time { struct date sdate; struct time stime; };
7
typedef A way of identifying a programmer-defined data type Avoids proceeding all struct declarations with the word struct Allows struct to be passed as a parameter into a function Variables can be created of a type once it has been defined using typedef
8
Initialising structures Consider, struct date { int month, day, year; }; Values are listed inside a pair of braces with each separated by a comma corresponding to the fields in the struct. struct date today = { 4,23,1998 };
9
Assigning values to structure fields Each field can be separately assigned. Assignments conform to the standard for simple variables of that type st_record.age = 19; st_record.year = 2000; st_record.year will behave just like a normal integer. However, it is referred to by st_record.name Here the dot is an operator that selects a field from a structure.
10
Structures as Function Arguments A structure can be passed as a function argument just like any other variable. Where we wish to modify the values of fields of the structure we must pass a pointer to that structure. This is just like passing a pointer to an int type argument whose value we wish to change. If we are only interested in one field of a structure it is probably simpler to just pass that field. This will make for a simpler function which is easier to re-use. Of course, if we wish to change the value of that field we should pass a pointer to it. When a structure is passed as an argument each member of the structure is copied. Passing and working with pointers to large structures may be more efficient in such cases.
11
You may:Example Initialise structure fieldsstruct student { char group; int mark; } tom = {‘A’, 65}, dick = {‘M’, 60}; access membersn = tom.mark; copy an entire structuretom = dick; take a structure addressp = &tom; printf(“%d”,(*p).mark; use structures as parameter n = function(dick); You may NOT compare structures logically e.g. if (tom = = dick) is not allowed.
12
Example #include typedef struct { char ID; float distance, radius; } planet; planet mars; int main() { mars.name = ‘M’); mars.distance = 1.5; mars.radius = 0.4; printf("Planetary statistics:\n"); printf("Name: %c\n",mars.name); printf("Distance from Sun in AU: %4.2f\n",mars.distance); printf("Radius in Earth radii: %4.2f\n",mars.radius); return 0; }
13
Data Collections Involving Multiple Types Two main methods Arrays in parallel containing differing data types Arrays where each element contains a collection of data types
14
Parallel Arrays Corresponding position in each array refers to a different piece of data which is an item of data belonging to the same logical entity e.g. Name (string) Age (integer) Grade (character) Number of Attendances (int) 0 1 2 3 4 5
15
Data Collections Involving Multiple Types Requires of an array of structures capable of holding complex data. Referred to in C as a struct. In the example below struct student may be defined Each element of the array contains a collection of data of the same format. Name Age Grade Attendances
16
Array of Structs name age grade attendances Name Age Grade Attendances Name Age Grade Attendances Name Age Grade Attendances An array of student structs students[2] students[0] 0 1 2 3 The definition of each element is referred to as a struct student
17
Arrays of Structures An array called birthdays of the same data type as the structure date can be created thus: struct student students[5]; This creates an array of 5 elements each of type date. Assignments and operations can be carried out thus: students[1].age = 20; --students[1].attendances;
18
Arrays of Structs Variables are declared as being instances of the user-defined type in the same way that variables of pre-defined types are declared (using a slightly different notation however!!) An array can be composed of Structs to give an indexed collection of objects that consist of mixed data studentarray[1] = student (copy a struct into a position in an array) studentarray[1].age = 21(update part of an element of an array of structs
19
Arrays of Structures An array called birthdays of the same data type as the structure date can be created thus: struct date birthdays[5]; This creates an array of 5 elements - each of type date. Assignments and operations can be carried out thus: e.g birthdays[1].month = 12; --birthdays[1].year;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.