Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structure.

Similar presentations


Presentation on theme: "Structure."— Presentation transcript:

1 Structure

2 Structure – aggregate type
The form of a structure is struct struct_name { var_type var1; var_type var2; . . . }; where struct is the keyword for defining a structure struct_name is the name for this structure (since there may be more than one) var1, var2, ... are the specific variables that make up the structure

3 Structure To create a structure variable of the type struct_name, we would use this: struct struct_name variable_name; We are allocating memory for a variable called variable_name. The type of variable is a structure, specifically a struct_name structure. This is analogous to creating other types of variables,for example,int sum;

4 We can think of a structure as being a template for an
object. For example, we may wish to create a structure for storing information about a person: struct person { char name[20]; int age; double weight; }; In our program, we might create two variables using this structure: struct person John = {"John Smith", 25, 170.5}; struct person Mary = {"Mary Jones", 32, 120};

5 Accessing Structure Members
Once a structure variable is created, we can access specific members using a period, known as the structure member operator. struct person John = {"John Smith", 25, 170.5}; printf("%s is %d years old.", John.name, John.age);

6 Initializing Structure Variables
As we have learned, we can declare the variable and later initialize it: int x; x = 5; We can do the same with structures: struct person UTAstudent; strcpy(UTAstudent.name, “Mary Steele"); UTAstudent.age = 21; UTAstudent.weight = 115;

7 Initializing Structure Variables
WARNING: It would have been illegal to do the following: #include <stdio.h> #include <string.h> struct person { char name[15]; int age; }; int main(void) struct person John, Mary; John = {"John Smith", 25}; /* too late to do this way */ Mary = {"Mary Jones", 32}; printf("%s is %d\n", John.name, John.age); printf("%s is %d\n", Mary.name, Mary.age); }

8 Array as Structure Elements
We can have arrays as elements of a structure also: struct arrayStruct { int data[2][3]; double values[4]; }; int main(void) struct arrayStruct numbers = { {{1, 2, 3}, {4, 5, 6}}, {10, 20, 30 ,40} }; int i, k; for(i = 0; i < 2; i++) for(k = 0; k < 3; k++) printf("%d ", numbers.data[i][k] ); for(i = 0; i < 4; i++) printf("%f ", numbers.values[i] ); }

9 Arrays of Structure Instead of creating many structure variables individually, we can create an array of structures. #include <stdio.h> struct id { char name[20]; int age; }; int main(void) struct id person[2] = { {"John Smith", 25}, {"Mary Jones", 32} }; int i; for(i = 0; i < 2; i++) printf("%s, %d\n", person[i].name, person[i].age ); }

10 Operations on Structures
Very few operations may operate on a structure as a whole. The assignment operator The address operator sizeof() //may not be what you expected! alignment-padding/ Note: you cannot use == or != to compare two structures. If you want to compare them, you need to write your own function to compare the value of each member.

11 Structures and Functions
We can pass either a copy of a structure or a pointer to a structure to a function. Structures can also be the return type of a function. However, passing copies of structures requires more memory, which may be an issue in some situations.

12 struct person fx(struct person a) { struct person old = {"Tom", 20};
#include <stdio.h> #include <string.h> struct person { char name[50]; int age; }; struct person fx(struct person a); int main(void) { struct person UTAstudent = {"Mary Steele", 25}; struct person newa, temp; printf("%s is %d years old\n", UTAstudent.name, UTAstudent.age); UTAstudent.age = 99; strcpy(UTAstudent.name, "Bob"); newa = fx(UTAstudent); printf("%s is %d years old\n", newa.name, newa.age); temp = UTAstudent; UTAstudent = newa; newa = temp; printf("was UTAstudent: %s is %d years old\n", UTAstudent.name, UTAstudent.age); printf("was Tom: %s is %d years old\n", newa.name, newa.age); } struct person fx(struct person a) { struct person old = {"Tom", 20}; old.age += a.age; return old; } Output: Mary Steele is 25 years old Mary Steele is 99 years old Bob is 99 years old Tom is 119 years old was UTAstudent: Tom is 119 years old was Tom: Bob is 99 years old

13 #include <stdio.h> struct automobile { char make[50]; char model[50]; int year; }; void fx(struct automobile c[], int rows); void fx2(struct automobile d); int main(void) { struct automobile cars[] = {{"Ford", "Mustang", 2010}, {"Chevy", "Tahoe", 2004}, {"Chevy", "Impala", 1966} }; struct automobile mycar = {"Mazda", "Mazda3", 2009}; int rows = sizeof(cars)/sizeof(cars[0]); int i; printf("%d %s %s\n", cars[0].year, cars[0].make, cars[0].model); fx(cars, rows); fx2(mycar); for(i = 0; i < rows; i++) fx2(cars[i]); } void fx2(struct automobile d) { printf("I own a %s %s\n", d.make, d.model); } void fx(struct automobile c[], int rows) int i; for(i = 0; i < rows; i++) printf("%s %s\n", c[i].make, c[i].model);

14 Pointers as Structure Members
We can have pointers as members of structures as well as pointers to structures. This requires that we pay careful attention to the precedence of the dereference and structure member operators. We should keep in mind the following precedence rules: ◦ . member selection via object name left-to-right ◦ -> member selection via pointer left-to-right ◦ * dereference right-to-left The dereference operator has lower precedence than the member selection operators.

15 Pointers as Structure Members
The structure member operator has higher precedence than the dereference operator. struct node { int x; int *ptr; }; int main(void) { struct node temp; int age = 45; temp.x = 99; temp.ptr = &age; printf("%d, %d\n", temp.x, *temp.ptr); /* prints: 99, 45 */ printf("%d, %d\n", temp.x, *(temp.ptr) ); /* equivalent */ }

16 Pointers to Structure struct node { int x; int *ptr; }; int main(void) { struct node temp; struct node *sp = &temp; int age = 45; temp.x = 99; temp.ptr = &age; printf("using sp: %d, %d\n", (*sp).x, *(*sp).ptr); printf("using sp: %d, %d\n", sp->x, *sp->ptr); /* alternate */ }

17 Pointers to Structure (cont.)
Here’s how the previous code works: /* sp is a pointer to a node structure. the parentheses are necessary to dereference the pointer before accessing the member at the address */ printf("using sp: %d, %d\n", (*sp).x, *(*sp).ptr); /* the -> replaces the (*sp) notation */ printf("using sp: %d, %d\n", sp->x, *sp->ptr); prints using sp: 99, 45

18 Structure pointer as structure member
Not legal to have a structure of the same type as a member inside the declaration of the structure, however, a pointer to a structure is allowed. struct node{ int data; struct node y; } Wrong! struct node{ int data; struct node * y; } Correct. Here linked list comes!

19 Another Structure as Structure Member
#include <stdio.h> #include <string.h> struct b { char name[50]; int age; }; struct a { int year; struct b inner; int main(void) { struct b s1; struct a s2; strcpy(s1.name, "cat"); s1.age = 99; printf("%s, %d\n", s1.name, s1.age); s2.year = 2012; strcpy(s2.inner.name, "dog"); s2.inner.age = 1234; printf("%d, %s, %d\n", s2.year, s2.inner.name, s2.inner.age); s2.inner = s1; }


Download ppt "Structure."

Similar presentations


Ads by Google