struct safearr unsigned int len int array1024 void
- Slides: 31
Массив с проверкой выхода за границы struct safearr{ unsigned int len; int array[1024]; }; void init(struct safearr *arr){ arr->len = 0; } int add_elem(struct safearr *arr, int val){ int max = sizeof(arr->array)/sizeof(arr->array[0]); if( arr->len + 1 < max ){ arr->array[arr->len++] = val; return 0; } return -1; } int get_elem(struct safearr *arr, int index, int *val){ if( arr->len > index ){ *val = arr->array[index]; return 0; } return -1; } © Кафедра вычислительных систем ГОУ ВПО «Сиб. ГУТИ» 7
Список head 1 struct list { int val; struct list *next; }; 2 3 NULL Недопустимо: struct bad_struct { struct bad_struct val; }; Допустимо: struct ok_struct { struct ok_struct *ptr; }; © Кафедра вычислительных систем ГОУ ВПО «Сиб. ГУТИ» 8
Инициализация структур 1. Инициализация в порядке следования элементов struct address{ char counrty[64]; int index; char city[64]; char street[64]; int build, flat; } a = { "Russia", 630102, "Novosibirsk", "Kirova", 86, 402 }; 2. Инициализация элементов в произвольном порядке struct address{ char counrty[64]; int index; char city[64]; char street[64]; int build, flat; } b = {. index = 630102, . street = "Kirova", . build = 86}; © Кафедра вычислительных систем ГОУ ВПО «Сиб. ГУТИ» 9
Вложенность структур struct person{ char name[64]; char lastname[64]; char patronymic[64]; unsigned char age; struct address addr; } var; struct address{ char counrty[64]; int index; char city[64]; char street[64]; int build, flat; }; struct person var, *ptr = &var; sprintf(var. name, "Ivan"); sprintf(var. lastname, "Ivanov"); ptr->age = 25; ptr->addr. build = 86; sprintf(var. addr. city, "Novosibirsk"); © Кафедра вычислительных систем ГОУ ВПО «Сиб. ГУТИ» 15
Макрос offsetof #include <stdio. h> Содержит определение #include <stddef. h> макроса offsetof struct test{ int i; char j; double k; }; $. /offsetof int main() Offset of: i=0, j= 4, k=8 { Offset of (Self): j= 4 struct test t; printf("Offset of: i=%d, j= %d, k=%dn", offsetof(struct test, i), offsetof(struct test, j), offsetof(struct test, k) ); printf("Offset of (Self): j= %dn", (size_t)(&((struct test *)0)->j)); } © Кафедра вычислительных систем ГОУ ВПО «Сиб. ГУТИ» 16
container_of (2) #include <stddef. h> #define container_of(ptr, type, member) ({ char *mptr = (char*)(ptr); (type *)(mptr - offsetof(type, member) ); }) (char*)member - offsetof(type, member) member i s s 1 offsetof(type, member) © Кафедра вычислительных систем ГОУ ВПО «Сиб. ГУТИ» 17
container_of #include <stdio. h> #include <stddef. h> #define container_of(ptr, type, member) ({ char *__mptr = (char*)(ptr); (type *)(__mptr - offsetof(type, member) ); }) struct test{ struct test t[] = { char name[256]; {"Ivan", 20, 180. 1, 81 }, . . . }; unsigned char age; process(&t[0]. height); double height, weight; Height is not integer. Fix it. }; New info: void process(double *h) name = Ivan { age = 20 if( *h - (int)*h > 0 ){. . . struct test *p; printf("Height is not integer. Fix it. n"); *h = (int)*h; p = container_of(h, struct test, height); printf("New info: ntname = %sntage = %d" "ntheight = %fntweight = %fn", p->name, p->age, p->height, p->weight); } } © Кафедра вычислительных систем ГОУ ВПО «Сиб. ГУТИ» 18
container_of (3) int main() { struct test t[] = { {"Ivan", 20, 180. 1, 81 }, {"Alex", 21, 190, 85 }, {. name="Jack", . height=175. 6, . age=22} }; int i; for(i=0; i<sizeof(t)/sizeof(t[0]); i++){ process(&t[i]. height); } return 0; } © Кафедра вычислительных систем ГОУ ВПО «Сиб. ГУТИ» 19
Структуры описания номерных знаков (2) struct passengers{ char c 1[2]; short num; short region; }; struct police{ char c 1; short num; short region; }; struct military{ short num; char c 1[2]; short region; }; © Кафедра вычислительных систем ГОУ ВПО «Сиб. ГУТИ» 24
- Public void drawsquare(int x, int y, int len)
- Int tree
- Typedef struct tree int info struct *left
- Struct node int data struct node* next
- Typedef struct tree int info struct *left
- Sum0
- Void swap
- Int f(int n)
- Len = strlen("harry potter"); the value of len is
- C copy struct to struct
- Public int divide(int a int b)
- Int max(int x int y)
- Interface calculator public int add(int a int b)
- Long double
- Warning
- Unsigned representation
- Long unsigned int c
- Typedef int element
- Signed vs unsigned int
- Char short
- Struct node int i float j
- Nodenext
- Typedef struct node
- Struct node int i float j
- Struct point int x y
- Struct point int x y
- Struct node int data
- Struct sample int a
- Typedef struct node
- Void main(void)
- Void setup
- Void main(void)