C programming Language Chapter 7 Structures 04 Copyright

  • Slides: 17
Download presentation
C programming Language Chapter 7: Structures ספטמבר 04 Copyright Meir Kalech 1

C programming Language Chapter 7: Structures ספטמבר 04 Copyright Meir Kalech 1

What is a Structure? n How can we define something like an array, where

What is a Structure? n How can we define something like an array, where each element contains all the information about student: name, age, telephone, address, etc. n What is the type of this sort of array? n We can define multiple data types, but for some students we have to define multiple parallel arrays - for each type. structure. n It turns out that we need is a n A structure is a data aggregate, consisting of elements of different types (these elements are called members or fields). ספטמבר 04 Copyright Meir Kalech 2

Structure Usage n Structures are widely used. For instance: Person Process File fname lname

Structure Usage n Structures are widely used. For instance: Person Process File fname lname address salary priority total time status id name device sector Block open mode ספטמבר 04 Copyright Meir Kalech 3

Structure Declaration n n As mentioned above, structure is a type defined by the

Structure Declaration n n As mentioned above, structure is a type defined by the programmer. In order to use a structure, we should declare it. Then we can use it as any other primitive type. Declaration: structure_name { members; }; For the declaration, no memory is allocated. Syntax comment: don’t forget the semicolon after the struct block (like for any declaration). ספטמבר 04 Copyright Meir Kalech 4

Structure Declaration struct Student { char name[20]; int id; }; In this example we

Structure Declaration struct Student { char name[20]; int id; }; In this example we declared a new type named Student and provided information on its structure. n Now we can define new variables of Student type, exactly as we define variables of primitive types: int x, y, *p. Int = NULL; struct Student std 1, std 2, *p. Student = NULL; Each one of the variables (std 1, std 2) contains 2 fields. n ספטמבר 04 Copyright Meir Kalech 5

Structure Initialization struct Student { char name[20]; int id; }; n Initialization can be

Structure Initialization struct Student { char name[20]; int id; }; n Initialization can be achieved via definition: struct Student std = {“arie”, 222}; n The initialization parameter list corresponds to the structure’s (member) fields. ספטמבר 04 Copyright Meir Kalech 6

Structure Member Operator. n We can’t refer to the structure as a whole. However,

Structure Member Operator. n We can’t refer to the structure as a whole. However, the structure member operator. is used to refer to each one of the structure’s fields. Reference a field via the variable std 1 using operator. : n Reference a field via the pointer p. Student: n Question : Why are the parentheses necessary? Another, more aesthetic way for referencing a field via a pointer is using the (shorthand for (*pointer). ) operator -> (arrow): n n std 1. name std 1. id (*p. Student). name (*p. Student). id p. Student->name p. Student->id ספטמבר 04 Copyright Meir Kalech 7

Example struct Student { std 1: 100 name: 100: ____ id: 120: ____ char

Example struct Student { std 1: 100 name: 100: ____ id: 120: ____ char name[20]; int id; }; void main() { std 2: 250 struct Student std 1, std 2, *p. Std; printf(“enter name and id of std 1 and std 2n”); scanf(“%s %d”, std 1. name, &(std 1. id)); scanf(“%s %d”, std 2. name, &(std 2. id)); p. Std = &std 1; printf(“%s %d”, p. Std->name, p. Std->id); } ספטמבר 04 Copyright Meir Kalech name: 250: ____ id: 270: ____ p. Std: 300 100 8

Array of Structures n n Definition: struct_name array_name[num_of_items] Example: struct Student students[3]; students[2]. id

Array of Structures n n Definition: struct_name array_name[num_of_items] Example: struct Student students[3]; students[2]. id = 222; (*(students+2)). id = 333; (students+2)->id = 444; students: 100 name: 100: ____ name: 124: ____ name: 148: ____ id: 120: ____ id: 144: ____ id: 168: ____ n n Initialization via definition: struct Student students[3] = { {“ami”, 222}, {“dana”, 333}, {“haia”, 444} If internal braces are missing, what will happen? ? ? ספטמבר 04 Copyright Meir Kalech }; 9

Pointer as Field n n A field may be a pointer also. We can

Pointer as Field n n A field may be a pointer also. We can refer to it by operator *(). or by ->, and then assign it to any address. For example: stack heap typedef struct { char *name; int id; } Student; void main() { Student std 1, std 2, *p. Std; std 2. name = “arie”; p. Std = &std 1; p. Std->name = (char *) malloc(5); strcpy(p. Std->name, ”arie”); } ספטמבר 04 std 1: 100 name: 100: 2700 id: 104: ____ std 2: 250 name: 250: 340 id: 254: ____ 2700 a r i e 340 a r i e p. Std: 300 100 Copyright Meir Kalech 10

Structure as Parameter to Function n n A structure can be passed to a

Structure as Parameter to Function n n A structure can be passed to a function by value or by address. Upon passing it by value, it is copied to the function. Any changes in the copy will not affect the original. Upon passing it by address, only its address is passed. Any changes on the pointed value will affect the original. For example: typedef struct { char name[20]; int id; } Student; void main() { Student std 1; input_student(&std 1); output_student(std 1); } ספטמבר 04 void input_student(Student *std) { scanf(“%s”, std->name); scanf(“%d”, &(std->id)); } void output_student(Student std) { printf(“%s”, std. name); printf(“%d”, std. id); } Copyright Meir Kalech 11

Nested Structures n n A member of a structure may be itself a structure.

Nested Structures n n A member of a structure may be itself a structure. For example, a student could be a member of a class. typedef struct { char name[20]; int id; } Student; typedef struct { Student students[3]; int num_students; char teacher[20]; } Class; void main() { school: 100 name: id: num_students: teacher: 100: _ 124: _ 148: _ 168: _ 196: _ 216: _ 220: _ 244: _ 268: _ 172: _ 176: _ 272: _ Class school[2]; } ספטמבר 04 Copyright Meir Kalech 12

Nested Structures Access to the fields of Class can be done via the structure

Nested Structures Access to the fields of Class can be done via the structure member operator. or via -> as well as the access to Student’s fields. void main() { Class school[2]; school[1]. num_students = 2; } school: 100 name: id: num_students: teacher: ספטמבר 04 100: _ 124: _ 144: _ 172: _ 148: _ 168: _ 196: _ 216: _ 268: 176: _ Copyright Meir Kalech 220: _ 244: _ 264: _ 2 272: _ 13

Nested Structures Access to the fields of Class can be done via the structure

Nested Structures Access to the fields of Class can be done via the structure member operator. or via -> as well as the access to Student’s fields. void main() { Class school[2]; school[1]. num_students = 2; school[1]. students[1]. id = 4; } school: 100 name: id: num_students: What is its type? teacher: 100: _ 124: _ 144: _ 172: _ 148: _ 168: _ 196: _ 220: _ 216: _ 240: 4 268: 176: _ 244: _ 264: _ 2 272: _ Class ספטמבר 04 Copyright Meir Kalech 14

Nested Structures Access to the fields of Class can be done via the structure

Nested Structures Access to the fields of Class can be done via the structure member operator. or via -> as well as the access to Student’s fields. void main() { Class school[2]; school[1]. num_students = 2; school[1]. students[1]. id = 4; } school: 100 name: id: num_students: What is its type? teacher: 100: _ 124: _ 144: _ 172: _ 148: _ 168: _ 196: _ 220: _ 216: _ 240: 4 268: 176: _ 244: _ 264: _ 2 272: _ Student ספטמבר 04 Copyright Meir Kalech 15

Nested Structures n n Type recognition is very important for passing a structure as

Nested Structures n n Type recognition is very important for passing a structure as argument to a function. For instance: passing the classes to input and output functions. void input_class(Class *cls) { int i; scanf(“%s”, cls->teacher); void main() scanf(“%d”, &(cls->num_students)); { for(i=0; i<cls->num_students; i++) Class school[2]; input_student(&(cls->students[i])); int i; } for(i=0; i<2; i++) void output_class(Class cls) input_class(&school[i]); { //input_class(school+i); int i; for(i=0; i<2; i++) puts(cls. teacher); output_class(school[i]); for(i=0; i<cls. num_students; i++) } output_student(cls. students[i]); } ספטמבר 04 Copyright Meir Kalech 16

Nested Structures n Example of dynamic allocation of structures: typedef struct { void main()

Nested Structures n Example of dynamic allocation of structures: typedef struct { void main() { int i, j; Education edu; puts(“how many classes? ”); } Student; scanf(“%d”, &edu. num_classes); typedef struct edu. classes = (Class *) malloc(edu. num_classes*sizeof(Class)); { Student *students; for(i=0; i<edu. num_classes; i++) int num_students; { puts(“enter num of students”); char teacher[20]; scanf(“%d”, &(edu. classes[i]. num_students)); } Class; edu. classes[i]. students = (Student *) malloc typedef struct (edu. classes[i]. num_students*sizeof(Student)); { for(j=0; j<edu. classes[i]. num_students; j++) Class *classes; input_student(&(edu. classes[i]. students[j])); int num_classes; //input_student(((edu. classes)+i)->students+j); } } Education; char name[20]; int id; ספטמבר 04 } Copyright Meir Kalech 17