
C_Kurs_Lekt / C_II_семестр / 07-1_Примеры работы со структурами
.pdfЛысый Д.А. Основыпрограммирования. Примеры работы со структурами (доступ через указатели) |
1 |
Примеры работы со структурами (доступ через указатели)
#include <stdio.h> |
/* для функций ввода-вывода*/ |
|
||||
#include <math.h> |
/* для математических функций */ |
|
||||
#include <stdlib.h> |
/*free, rand ... |
*/ |
|
|
||
#include <conio.h> |
/* для функции очистки экрана clrscr() */ |
|||||
typedef struct { |
|
|
|
|
||
char |
name[15]; |
|
|
|
|
|
int |
old; |
} igra; // тип (структура одного игрока) |
|
|||
int main () |
|
|
|
|
|
|
{ |
|
|
|
|
|
|
int d; |
|
|
|
|
|
|
int * pd; |
|
|
|
|
|
|
int mas[10]; |
|
|
|
|
||
igra foot; |
|
|
|
|
|
|
igra * pfoot; |
|
|
|
|
||
igra mfoot[10]; |
|
|
|
|
||
igra * pmfoot[5]; |
|
|
|
|
||
char str1[30]; |
|
|
|
|
||
int i; |
|
|
|
|
|
|
clrscr(); |
/* очистка экрана */ |
|
|
|
||
/* |
printf(" enter d "); |
|
|
|
||
scanf("%d", &d); |
|
|
|
|
||
printf("you enter d = %d", d); |
|
|
|
|||
pd = &d; |
|
|
|
|
|
|
printf("\n enter pd "); |
|
|
|
|||
scanf("%d", pd); |
|
|
|
|
||
printf("you enter d = %d", *pd); |
|
|
|
|||
if ((pd = (int *) calloc(5, sizeof(int))) == NULL) |
{ |
|||||
|
printf("Ошибка выделения памяти") ; |
exit(2); } |
||||
for ( i=0;i<2;i++) { |
|
|
|
printf("\n enter pd[%d] ",i); scanf("%d", &pd[i]);
printf("you enter d[%d] = %d", i, pd[i]);
}
for ( i=0;i<2;i++) {
printf("\n enter mas[%d] ",i); scanf("%d", &mas[i]);
printf("you enter mas[%d] = %d", i, mas[i]);
}
*/
//struct's
//igra foot;
/*
printf(" enter foot.old "); scanf("%d", &foot.old);
printf("\t you enter foot.old = %d\n", foot.old);
printf(" enter foot.name (fgets) "); fflush(stdin); //очистим буфер fgets(foot.name, 15, stdin);
printf("\t you enter foot.name(fgets) = %s", foot.name);
Лысый Д.А. Основыпрограммирования. Примеры работы со структурами (доступ через указатели) |
2 |
printf(" enter foot.name (scanf) "); scanf("%s13", foot.name);
printf("\t you enter foot.name = %s\n", foot.name); */
/*
//igra * pfoot; // как указатель на (одну) структуру
if (( pfoot = (igra *) calloc(1, sizeof(igra))) == NULL) { |
|
||||||||
|
printf("Ошибка |
выделения |
памяти |
под |
|
pfoot") |
; |
||
exit(2); } |
|
|
|
|
|
|
|
|
|
printf(" enter pfoot->old ",i); |
|
|
|
|
|||||
scanf("%d", &pfoot->old); |
|
|
|
|
|
|
|||
printf("\t you enter pfoot->old = %d\n", pfoot->old); |
|
||||||||
printf(" enter pfoot->name |
",i); |
|
|
|
|
||||
scanf("%s", pfoot->name); |
|
|
|
|
|
|
|||
printf("\t you enter pfoot.name = %s\n", pfoot->name); |
|
||||||||
free(pfoot); |
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
// |
igra * pfoot; // как указатель на массив структур |
|
|||||||
/* |
|
|
|
|
|
|
|
|
|
|
if (( pfoot = (igra *) calloc(4, sizeof(igra))) == NULL) { |
|
|||||||
|
printf("Ошибка выделения памяти под pfoot") ; |
|
exit(2); } |
||||||
|
for(i=0; i < 2; i++) { |
|
|
|
|
|
|
||
|
printf(" enter pfoot[%i].old |
",i); |
|
|
|
|
|||
|
scanf("%d", &pfoot[i].old); |
|
|
|
|
|
|||
|
printf("\t you enter pfoot[%d].old = %d\n", i, pfoot[i].old); |
||||||||
|
} |
|
|
|
|
|
|
|
|
|
for (i=0; i < 2; i++) { |
|
|
|
|
|
|
||
|
printf(" enter pfoot[%i].name |
",i); |
|
|
|
|
|||
|
scanf("%s", pfoot[i].name); |
|
|
|
|
|
|||
|
printf("\t |
you |
enter |
pfoot[i].name |
= |
%s\n", |
i, |
||
pfoot[i].name); |
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
free(pfoot); |
|
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
// |
igra mfoot[10]; |
|
|
|
|
|
|
|
|
/* |
|
|
|
|
|
|
|
|
|
|
for(i=0; i < 2; i++) { |
|
|
|
|
|
|
||
|
printf(" enter mfoot[%i].old |
",i); |
|
|
|
|
|||
|
scanf("%d", &mfoot[i].old); |
|
|
|
|
|
|||
|
printf("\t you enter mfoot[%d].old = %d\n", i, mfoot[i].old); |
||||||||
|
} |
|
|
|
|
|
|
|
|
|
for (i=0; i < 2; i++) { |
|
|
|
|
|
|
||
|
printf(" enter mfoot[%i].name |
",i); |
|
|
|
|
|||
|
scanf("%s", mfoot[i].name); |
|
|
|
|
|
|||
|
printf("\t |
you |
enter |
mfoot[%d].name |
= |
%s\n", |
i, |
||
mfoot[i].name); |
|
|
|
|
|
|
|
|
}
*/
Лысый Д.А. Основыпрограммирования. Примеры работы со структурами (доступ через указатели) |
|
3 |
|
// |
igra * pmfoot[5]; igra - массив из 5 указателей на igra |
||
// аналогично igra ** pmfoot |
|
|
|
|
for(i=0; i < 5; i++) |
|
|
|
if (( pmfoot[i] = (igra *) calloc(1, sizeof(igra))) == NULL) { |
||
|
printf("Ошибка выделения памяти под |
pmfoot[%d}",i) ; |
|
exit(2); } |
|
|
|
|
for(i=0; i < 2; i++) { |
|
|
|
printf(" enter pmfoot[%i]->old |
",i); |
|
|
scanf("%d", &pmfoot[i]->old); |
|
|
|
printf("\t you enter pmfoot[%d]->old = %d\n", i, pmfoot[i]- |
||
>old); |
|
|
|
|
} |
|
|
|
for (i=0; i < 2; i++) { |
|
|
|
printf(" enter pmfoot[%i]->name |
",i); |
|
|
scanf("%s", pmfoot[i]->name); |
|
|
printf("\t you enter pmfoot[%d]->name = %s\n", i, pmfoot[i]- >name);
}
for(i=0; i < 5; i++) free(pmfoot[i]);
printf("\n press any key to continue...\n"); getch();
return 0;
}