카테고리

asm (27) bootloader_x86_grub (1) C (92) compile (11) config (76) CPP (13) CSS (1) debugging (7) gimp (1) Go (1) html (1) Java (1) JavaScript (1) kernel (19) LibreOffice (3) Linux system progamming (21) MFC (1) opencv (4) OpenGL (1) PHP (1) Python (4) qemu (29) shell (3) socket (7) troubleshooting (2) ubuntu18.04 (2) windows (1)

2018/12/16

C 구조체 정의


1. C의 구조체(struct)
- Struct(구조)
하나 이상의 변수 그룹
각 변수는 다른 유형을 가질 수 있다.

#include <stdio.h>

struct student {
char *pLastname;
char *pFirstname;
int i32StrudentID;
float fGPA;
};
student
자료형
변수 이름
크기
char *
pLastname
32bit
char *
pFirstname
32bit
int
i32StrudentID
32bit
float
fGPA
32bit
32bit * 4 = 128


int main(void){

struct student student1;
struct student
자료형
student1;
크기
구조체
32bit
32bit
32bit
32bit
32 * 4

32bit * 4 = 128bit

struct student classA[10];
struct student
자료형
classA[10];
크기
구조체
0
128bit
1
128bit
2
128bit
3
128bit
4
128bit
5
128bit
6
128bit
7
128bit
8
128bit
9
128bit
128 * 10

128bit * 10 = 1280bit

struct student *classB;

struct student
자료형
*classB;
크기
주소
32bit 주소
32bit

32bit * 1 = 32bit
};

구조체 멤버 접근 방법.

#include <stdio.h>

struct student{
char *pLastname;
char *pFistname;
int i32StudentID;
float fGPA;
};

student
자료형
변수 이름
크기
char *
pLastname
32bit
char *
pFirstname
32bit
int
i32StrudentID
32bit
float
fGPA
32bit
32bit * 4 = 128

int main(void){

struct student student1;
struct student
자료형
student1;
크기
구조체
32bit
32bit
32bit
32bit
32 * 4

32bit * 4 = 128bit

struct student classA[10];
struct student
자료형
classA[10];
크기
구조체
0
128bit
1
128bit
2
128bit
3
128bit
4
128bit
5
128bit
6
128bit
7
128bit
8
128bit
9
128bit
128 * 10

128bit * 10 = 1280bit

struct student *classB;
struct student
자료형
*classB;
크기
주소
32bit 주소
32bit

32bit * 1 = 32bit

student1.pLastname="Hong";
student1
자료형
char *pLastname
크기
char *
pLastname
Hong
32bit

student1.pFistname="Gildong";
student1
자료형
char *pLastname
크기
char *
pFistname
Gildong
32bit

student1.i32StudentID=20180101;
student1
자료형
int i32StudentID
크기
int
i32StudentID
20180101
32bit

student1.fGPA=3.68;
student1
자료형
float fGPA;
크기
int
fGPA;
3.68
32bit

classA[0].i32StudentID=20160305;
classA
자료형
[0]
크기
struct
i32StudentID
20160305;
128bit

classB->fGPA=4.1;
classB
자료형
float fGPA;
크기
주소
fGPA;
4.1;
32bit
return 0;
};

구조체 확장
struct club{
char *pName;
int i32Number;
struct student aMembers[50];

};

#include <stdio.h>

int main(void){
struct club aClub[4];
}

학교, 클럽, 동호회, 지역 사회, 작은 의미를 가지는 구조체를 정의해 사용, 작은 구조체를 포괄하여 확장 시키면 클럽이 형성되어지고 클럽이 더욱 커지면 동호회가 만들어 진다.


댓글 없음:

댓글 쓰기