Code Archive/C언어

Step11. 구조체(struct)

쌍큐 2014. 9. 12. 13:01
 - 데이터의 집합체
- 상품 : 상품명, 가격, 바코드......
코드로 실제 데이터에 해당하는 대상을 표현한 것으로
하나의 객체가 가질수 있는 변수의 집합체
 

 

기본 문법>
struct 구조체명{

	필요한 변수 선언;
    
};

typedef struct 구조체명{

	필요한 변수 선언;

}별칭;
 

기본 예제>

#include <stdio.h>
//구조체(struct)
//구조체 문법
/*
struct 구조체명{
	필요한 변수 선언;
	....
};
*/
struct person {
	char name[30];
	int age;
};
int main(void) {
	struct person p;
	printf("나이 입력 : ");
	scanf("%d", &p.age);
	printf("이름 입력 : ");
	scanf("%s", p.name);

	printf("이름 :%s, 나이 : %d\n", p.name, p.age);

	printf("sizeof(struct person) : %d\n", sizeof(struct person));
	return 0;
}
초기화>
#include <stdio.h>

struct person {
	char name[28];
	int age;
};

int main(void) {
	//구조체 초기화는 구조체 안에 선언 변수 순서대로 값을 나열
	struct person p = {"홍길동",20};
	printf("p name address : %p\n", p.name);
	printf("p age address : %p\n", &p.age);
	printf("name : %s, age : %d\n", p.name, p.age);
	char name[28] = "김철수";
	int age = 10;
	struct person s;
	//구조체에 데이터를 저장, 문자열이나 포인터일 경우에는 동적 할당한 후에 복사, 문자열 복사
	strcpy_s(p.name, sizeof(p.name), name);
	p.age = age;

	return 0;
}
 
동적할당 응용>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct person {
	char* name;
	int age;
};
//person 하나를 생성해서 리턴
struct person* createPerson() {
	//person 하나 동적할당
	struct person* ptr = (struct person*)malloc(sizeof(struct person));
	char name[30];
	int age;
	printf("이름 입력 : "); scanf_s("%s", name, sizeof(name));
	printf("숫자 입력 : "); scanf_s("%d", &age);
	(*ptr).age = age;//동적 할당한 person의 age에 저장
	ptr->age = age;
	ptr->name = (char*)malloc(strlen(name) + 1);//+1은 마지막 널문자 저장할 공간
	strcpy_s(ptr->name, strlen(ptr->name), name);
	return ptr;
}
int main(void) {
	struct person* p = createPerson();
	printf("%s, %d\n", p->name, p->age);
	return 0;
}
 
 
typedef 활용>
#include <stdio.h>
#include <malloc.h>
struct person {
	char name[28];
	int age;
};
typedef int* PINT;
typedef unsigned int UINT;
typedef unsigned int* P_UINT;
typedef struct person Person;

typedef struct {
	char* sno;
	char* name;
}Student;

int main(void) {
	//unsigned int n;
	UINT n;
	//unsigned int *ptr;
	P_UINT ptr;
	return 0;
}
 
구조체 포인터 사용>
#include <stdio.h>
//구조체 안에 자기 자신 타입의 포인터를 선언하는 경우
typedef struct position {
	int xpos;
	int ypos;
	struct position* next;
}Position;
int main(void) {
	Position p1 = { 1,2,NULL };
	Position p2 = { 3,5,NULL };
	Position p3 = { 7,0,NULL };
	p1.next = &p2;
	p2.next = &p3;
	
	Position* p = &p1;
	//p1부터 마지막 Position까지 접근하는 반복문
	while (p != NULL) {
		printf("%d, %d\n", p->xpos, p->ypos);
		p = p->next;
	}
	
	return 0;
}
 
구조체 in 구조체>
#include <stdio.h>
typedef struct position {
	int xpos;
	int ypos;
}Position;

typedef struct circle {
	Position pos;
	int r;
}Circle;

int main(void) {
	Circle c;
	printf("%p %p %p %d\n", &c.pos.xpos, &c.pos.ypos, &c.r,sizeof(Circle));
	//구조체 배열 선언 및 초기화
	Position arr[3] = { {10,3},{1,5},{2,5} }; //2차원 배열 초기화 하는 것과 동일
	int i;
	for (i = 0; i < 3; i++) {
		printf("%d,%d\n", arr[i].xpos, arr[i].ypos);
	}
	//Circle 배열 3개짜리 선언 후 초기화, 전체 출력
	Circle cir[3] = {
		//x,y , r
		{{3,4},10},//cir[0]
		{{6,7},5},//cir[1]
		{{8,1},6},//cir[2]
	};
	for (i = 0; i < 3; i++) {
		printf("xpos : %d, ypos : %d, r : %d\n", 
			cir[i].pos.xpos, cir[i].pos.ypos, cir[i].r);
	}
	return 0;
}









 
예제1> 포인터 및 배열 사용하는 방법
#include <stdio.h>struct Product{ char p_name[20]; //char *p_name; int price; char grade;};void main(){ /* Product p; p.p_name = "휴대폰"; p.price = 1200; p.grade = 'A';
printf("%s %d %c\n",p.p_name,p.price,p.grade); */ Product arr[4]; int i; for(i=0;i<4;i++) scanf("%s",arr[i].p_name);
Product *ptr; ptr = &arr[0]; printf("%s",(*ptr).p_name); printf("%s",ptr->p_name);
}

 

'Code Archive > C언어' 카테고리의 다른 글

Step13. 열거형(enum)  (0) 2014.09.12
Step12. 동적 메모리 할당(malloc/realloc)  (0) 2014.09.12
Step10. Call by Value, Call by Reference  (0) 2014.09.06
Step9. 데이터 캐스팅  (0) 2014.09.06
Step8. 포인터  (0) 2014.09.06