🥇 정보처리 기사 실기 프로그래밍-구조체(structuer)
#include <stdio.h>
struct jsu {
char nae[12];
int os, db, hab, hhab;
};
int main() {
struct jsu st[3] = {
{"데이터1", 95, 88},
{"데이터2", 84, 91},
{"데이터3", 86, 75}
};
struct jsu* p;
p = &st[0];
(p + 1)->hab = (p + 1)->os + (p + 2)->db;
(p + 1)->hhab = (p + 1)->hab + p->os + p->db;
printf("%d", (p + 1)->hab + (p + 1)->hhab);
}
p = &st[0];
=> 구조체 st의 첫번째 객체의 주소값을 저장(p + 1)->hab = (p + 1)->os + (p + 2)->db;
(p+1)->hab
: st첫번째 객체의 다음객체(st[1]
)의 hab요소의 값st[1]->hab = 84 + 75
= 159(p + 1)->hhab = (p + 1)->hab + p->os + p->db;
구조체의 멤버를 지정할 때는 변수명 멤버이름 으로 지정하지만, [변수명].[멤버이름]
포인터 변수를 이용해 구조체의 멤버를 지정할 때는 변수명 멤버이름 으로 지정한다 [변수명]->[멤버이름]
struct Node {
int value;
struct Node* next;
};
void func(struct Node* node) {
while (node != NULL && node->next != NULL) {
int t = node->value;
node->value = node->next->value;
node->next->value = t;
node = node->next->next;
}
}
int main() {
struct Node n1 = {1, NULL};
struct Node n2 = {2, NULL};
struct Node n3 = {3, NULL};
n1.next = &n3;
n3.next = &n2;
func(&n1);
struct Node* current = &n1;
while (current != NULL) {
printf("%d", current->value);
current = current->next;
}
}
n1 - n3 - n2 순서
node->value = node->next->value;
이때 n1부터 연결된 노드의 벨류를 프린트하면 3 1 2