ch22

암영·2022년 6월 6일
0

c언어

목록 보기
17/21

22-1 구조체란 무엇인가
456-7예제
#include<stdio.h>
#include <string.h>

struct person {
char name[20];
char phonenum[20];
int age;
};
int main() {
struct person man1, man2;
strcpy_s(man1.name,20 , "안성준"); //문자열 길이 지정해 줘여함
strcpy_s(man1.phonenum ,20, "010-1122-3344");
man1.age = 23;

printf("이름입력:"); scanf_s("%s", man2.name,20);//문자열 길이 지정해 줘여함
printf("번호입력:"); scanf_s("%s", man2.phonenum,20);
printf("나이입력:"); scanf_s("%d",  &(man2.age));


printf("이름:%s\n", man1.name); 
printf("번호:%s\n", man1.phonenum); 
printf("나이:%d\n", man1.age); 

printf("이름:%s\n", man2.name);
printf("번호:%s\n", man2.phonenum);
printf("나이:%d\n", man2.age);



return 0;

}

#include <stdio.h>
struct point {
int xpos;
int ypos;
};

struct person {
char name[20];
char phonenum[20];
int age;
};

int main()
{
struct point pos = { 10,20 };
struct person man = { "이승기","010-1212-0001",21 };
printf("%d %d \n", pos.xpos, pos.ypos);
printf("%s %s %d\n", man.name, man.phonenum, man.age);
return 0;
}

22-2 구조체 배열과 포인터
#include <stdio.h>

struct point
{
int xpos;
int ypos;
};

int main()
{
struct point arr[3];
int i;
for (i = 0; i < 3; i++)
{
printf("점의 죄표 입력");
scanf_s("%d %d", &arr[i].xpos, &arr[i].ypos);
}

for (i = 0; i < 3; i++)
{
	printf("[%d %d]", arr[i].xpos, arr[i].ypos);
}
return 0;

}

#include <stdio.h>

struct person
{
char name[20];
char phonenum[20];
int age;

};

int main()
{
struct person arr[3]=
{
{"이승기","010-1212-0001",21},
{"정지영","010-1313-0002",22},
{"한지수","010-1717-0003",19}
};

int i;
for (i = 0; i < 3; i++)
	printf("%s %s %d\n", arr[i].name, arr[i].phonenum, arr[i].age);
return 0;

}

profile
just do! -얼레벌레 굴러가는 공대생

0개의 댓글