Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <locale.h>
- #include <stdlib.h>
- struct C42
- {
- char *name;
- int year, oc;
- char *hobby;
- };
- void init(C42 * p, const char * n, int y, int o, const char * h)
- {
- p->name = (char *)malloc(strlen(n) + 1);
- strcpy((*p).name, n); // -> strcpy(p->name, n);
- p->year = y;
- p->oc = o;
- p->hobby = (char *)malloc(strlen(h) + 1);
- strcpy(p->hobby, h);
- }
- void print(C42 * p)
- {
- printf("%20s %3d %3d \"%s\"\n", p->name, p->year, p->oc, p->hobby);
- }
- void print_array(C42 ** p, int sz)
- {
- int i;
- for(i=0;i<sz;i++)
- print(p[i]);
- }
- void init_array(C42 **p, int sz)
- {
- int i;
- for(i =0;i<sz;i++)
- p[i] = (C42 *)malloc(sizeof(C42));
- }
- void erase(C42** p, int sz)
- {
- int i;
- for(i=0;i<sz;i++)
- {
- free(p[i]->name);
- free(p[i]->hobby);
- free(p[i]);
- }
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- C42 *group[3];
- init_array(group, 3);
- init(group[0], "Петр Петров", 3, 4, "Компьютерные игры");
- init(group[1], "Василий Васильев", 2, 5, "Программирование");
- init(group[2], "Иван Иванович", 1, 0, "Мультипликация");
- print_array(group, 3);
- erase(group, 3);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement