1 / 9

# Structures II

Structures II. Soon Tee Teoh CS 49C. Initializing an array. A variable can be initialized when it is declared. Likewise, an array can be initialized. int x = 5; int arr = { 3, 2, 5};. Initializing a struct. Likewise, a struct can also be initialized.

Télécharger la présentation

## Structures II

E N D

### Presentation Transcript

1. Structures II Soon Tee Teoh CS 49C

2. Initializing an array • A variable can be initialized when it is declared. • Likewise, an array can be initialized. int x = 5; int arr = { 3, 2, 5};

3. Initializing a struct • Likewise, a struct can also be initialized. • Struct initialization looks similar to array initialization. struct mystruct { int x; float y; char z; }; struct mystruct s1 = { 3, 5.2, ‘z’ };

4. Initialization example #include <stdio.h> struct student; struct coord { float x, y, z; struct student *sptr; }; struct student { int id; struct coord pos; }; int main() { struct student s1 = { 3 , { 1.0, 0.0, 0.5, 0 } } ; struct coord c1 = { 3.3, 1.3, 2.2 , 0 }; printf("s1 is %d %f %f %f\n", s1.id, s1.pos.x, s1.pos.y, s1.pos.z); printf("c1 is %f %f %f\n", c1.x, c1.y, c1.z); return 0; }

5. Passing a struct as argument and returning a struct • Since a struct is just a type, you can pass it as argument, just like and int or float. • When we pass arguments by value, a copy of the argument is made when the function is called. • When we return a value, a new value is created and returned to the caller. int main() { struct student s1 = { 3 , { 1.0, 0.0, 0.5, 0 } } ; struct student s5; printf("s1 is %d %f %f %f\n", s1.id, s1.pos.x, s1.pos.y, s1.pos.z); s5 = func(s1); printf("s5 is %d %f %f %f\n", s5.id, s5.pos.x, s5.pos.y, s5.pos.z); printf("s1 is %d %f %f %f\n", s1.id, s1.pos.x, s1.pos.y, s1.pos.z); return 0; } struct student; struct coord { float x, y, z; struct student *sptr; }; struct student { int id; struct coord pos; }; Struct student func(student s0) { struct student s2; printf("s0 is %d %f %f %f\n", s0.id, s0.pos.x, s0.pos.y, s0.pos.z); s2.id = s0.id + 2; s2.pos = s0.pos; s0.id = s0.id + 10; printf("s0 is %d %f %f %f\n", s0.id, s0.pos.x, s0.pos.y, s0.pos.z); return s2; }

6. Component-wise copy • When a copy is made of a struct, each component of the struct is copied. • This happens during (1) assignment (the ‘=‘ operator), (2) passing an argument, and (3) returning from a function. struct mystruct { int i; int *iptr; }; int main() { struct mystruct m1, m2; m1.i = 5; m1.iptr = new int(3); m2 = m1; m2.i = 10; *(m2.iptr) = 15; printf("m1 is %d %d\n", m1.i, *(m1.iptr)); printf("m2 is %d %d\n", m2.i, *(m2.iptr)); return 0; }

7. Example: Linked List • List is a useful data structure. • Can add and delete from the list. • Different from allocating an array, since the size of an array has to be determined ahead of time. • Many types of lists. • Example: list of integers struct ListNode { int data; ListNode *next; };

8. Example: Linked List • To access to a list, just have a pointer to the first node of the list. • To denote the end of the list, set the next pointer of the last node to NULL (0). • Example below: L1 has two elements, L2 is empty. #include <stdio.h> #include <stdlib.h> struct ListNode { int data; struct ListNode *next; }; int main() { struct ListNode *L1 = 0; struct ListNode *L2 = 0; L1 = (ListNode *)malloc(sizeof(struct ListNode)); L1->data = 5; L1->next = (struct ListNode *)malloc(sizeof(struct ListNode)); L1->next->data = 10; L1->next->next = 0; return 0; }

9. Exercise • Write a function to print all the elements of a list. • Write a function to return the length of a list. • Write a function to add a number to a list. • Write a function to delete all occurrences of a number from a list. void PrintList (struct ListNode *L); int ListLength(struct ListNode *L); void AddToList(struct ListNode **L, int n); void DeleteFromList(struct ListNode **L, int n);

More Related