본문 바로가기
C/Example

[C] 구조체 예제

by 꾸압 2021. 3. 10.

<예제.1>

#include <stdio.h>
#include <string.h>

struct student{
	int number;
	char name[10];
};

int main(){
	
	struct student s1;
	
	s1.number = 1;
	strcpy(s1.name, "JJ");
	
	printf("%d", s1.number);
	printf("%s", s1.name);
	
	return 0;
}

 

 

<예제.2>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct date{
	int month;
	int day;
};

struct student{
	int number;
	struct date d;
	char name[10];
};

int main(){
	
	struct student s;
	
	s.number = 1;
	s.d.month = 3;
	s.d.day = 10;
	strcpy(s.name, "JJ");
	
	return 0;
}

◆date 구조체가 student 구조체 아래에 있으면 오류가 난다. 구조체 안에 구조체를 넣을 경우 '구조체의 입력 순서' 가 중요하다.

 

 

<예제.3> 구조체 간 대입

#include <stdio.h>
#include <stdlib.h>

struct student{
	int number;
	char name[10];
};

int main(){
	struct student s1 = { 1, "J_" };
	struct student s2 = { 2, "JJ" };
	
	s1 = s2;
	
	printf("%d %s \n", s1.number, s1.name);
	
	return 0;
}

 

 

<예제.4> 구조체 비교

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct student{
	int number;
	char name[10];
};

int main(){
	struct student s1 = { 1, "J_" };
	struct student s2 = { 2, "JJ" };
	
	s1 = s2;
	
	if(s1.number == s2.number && strcmp(s1.name, s2.name) == 0 ){
		printf("1111");
	} 
	
	else {
		printf("2222");
	}
	
	return 0;
}

 

 

<예제.5>

#include <stdio.h>
#include <string.h>

struct student{
	int number;
};

int main(){
	
	struct student s[5];
	int i;
	
	for(i=0; i<5; i++){
		s[i].number = i+1;
	}
	
	for(i=0; i<5; i++){
		printf(" %d ", s[i].number);
	}
	
	return 0;
}

 

 

<예제.6> 구조체 크기 계산 공식

#include <stdio.h>
#include <string.h>

struct student{
	int number;
};

int main(){
	
	struct student s[5];
	int i;
	
	int size = sizeof(s)/sizeof(struct student);
	
	printf("%d\n", size);
	
	for(i=0; i<size; i++){
		s[i].number = i+1;
	}
	
	for(i=0; i<size; i++){
		printf(" %d ", s[i].number);
	}
	
	return 0;
}

 

 

<예제.7.1> 구조체 포인터 쓰는 방법 : pS->변수

#include <stdio.h>
#include <string.h>
#define SIZE 5

struct student{
	int number;
};

int main(){
	
	struct student s[3] = { {1004}, {2004}, {3004} };
	
	struct student *pS = NULL;
	
	pS = s;
	
	int i;
	
	for(i=0; i<3; i++){
		printf("%d\n", (*(pS+i)).number);
		printf("%d\n", (pS+i)->number);
	}
	
	return 0;
}

 

 

<예제.7.2> 구조체 포인터 쓰는 방법

#include <stdio.h>
#include <string.h>

struct student{
	int number;
	int age;
};

int main(){
	
	struct student s = { 1, 2 };
	
	struct student *pS = NULL;
	
	pS = &s;
	
	printf("%d\n", *pS);
	printf("%d\n", (*pS).number);
	printf("%d\n", (*pS).age);
	
	printf("%d\n", pS->number);
	printf("%d\n", pS->age);
	
	return 0;
}

 

 

<예제.8>

#include <stdio.h>
#include <string.h>

struct student{
	int number;
	struct student *next;
};

int main(){
	
	struct student s1 = {1, NULL};
	struct student s2 = {2, NULL};
	struct student *first = NULL;
	struct student *current = NULL;
	
	first = &s1;
	s1.next = &s2;
	s2.next = NULL;
	current = first;
	
	while(current != NULL){
		printf(" %d ", current->number);
		current = current->next;
	}
	
	return 0;
}

 

 

<예제.9>

#include <stdio.h>
#include <stdlib.h>

int main(){
	
	int *score = NULL;
	int i;
	
	score = (int *) malloc (100*sizeof(int) );
	
	if(score == NULL){
		score[i] = i;
	}
	
	for(i=0; i<100; i++){
		score[i] = i;
	}
	
	for(i=0; i<100; i++){
		printf("  %d  ", score[i] );
	}
	
	free(score);
	
	return 0;
}

 

 

<예제.10>

#include <stdio.h>
#include <stdlib.h>

struct Book{
	int number;
};

int main(){
	
	struct Book *p;
	
	p = (struct Book *)malloc(2* sizeof(struct Book) );
	
	if(p == NULL){
		exit(1);
	}
	
	p[0].number = 1;
	p[1].number = 2;
	
	free(p);
	
	return 0;
}

 

 

<예제.11>  s2 -> s1 -> s4 -> s3 의 연결 짜기

#include <stdio.h>

struct student{
	int number;
	struct student *next;
};

int main(){ 
	
	struct student s1 = {1, NULL};
	struct student s2 = {2, NULL};
	struct student s3 = {3, NULL};
	struct student s4 = {4, NULL};
	struct student *start = NULL;
	struct student *current = NULL;
	
	start = &s2;
	s2.next = &s1;
	s1.next	= &s4;
	s4.next = &s3;
	s3.next	= NULL;
	current = start;
	
	while(current != NULL){
		printf("%d\n", current->number);
		current = current->next;
	}
	
	return 0;
}

 

'C > Example' 카테고리의 다른 글

[C] 구조체 예제  (0) 2021.03.12
[C] 문자열 예제  (0) 2021.03.09
[C] 포인터 예제  (0) 2021.03.09
[C] 좌석 예약 프로그램 (배열)  (0) 2021.03.08

댓글