두 구조체 값을 더하여 새로운 구조체에 대입
#include <iostream>
using namespace std;
struct Node
{
int x, y;
};
int main()
{
Node a = {1, 2}, b = {3, 4}, c;
c = {a.x+b.x, a.y + b.y};
cout << c.x << ' ' << c.y ;
return 0;
}
구조체 값 for문으로 대입
#include <iostream>
using namespace std;
struct Node
{
int x, y;
};
int main()
{
Node a[6];
for(int i=1; i<7; i++)
a[i] = {1, i};
for(int i=1; i<7; i++)
cout << a[i].x << ' ' << a[i].y << '\n';
return 0;
}
구조체 포인터로 값을 대입하고 변경
#include <iostream>
using namespace std;
struct Node
{
int x, y;
};
int main()
{
Node *a, *c, b;
a = &b;
c = &b;
(*a).x = 10;
c->x += 20;
cout << b.x;
return 0;
}
for문을 사용하여 구조체 배열 탐색
#include <iostream>
using namespace std;
struct Node
{
int x, y;
};
int main()
{
Node a[4] = {{5, 3}, {4,2}, {5,5}, {7,-5}};
for(int i=0; i<4; i++)
cout << a[i].x << ' ' << a[i].y << '\n';
return 0;
}
for문을 사용하여 구조체 배열 값 입력
#include <iostream>
using namespace std;
struct Node
{
int a, b, c[3];
};
int main()
{
Node abc[4];
for(int i=0; i<4; i++)
{
abc[i] = {3, 5+i};
for(int j=0; j<=2; j++)
abc[i].c[j] = i+1;
}
for(int i=0; i<4; i++)
{
cout << abc[i].a << ' ' << abc[i].b << ' ';
for(int j=0; j<3; j++)
cout << abc[i].c[j] << ' ';
cout << '\n';
}
return 0;
}
p를 이용하여 10, 20 넣기, q를 이용하여 50, 60 넣기
#include <iostream>
using namespace std;
struct Node
{
int x, y;
};
int main()
{
Node a, b, *p, *q;
p = &a;
q = &b;
p->x = 10;
p->y = 20;
q->x = 50;
q->y = 60;
return 0;
}
포인터 배열과 배열 포인터의 차이
포인터 배열 : int *arr[4]; -> 배열 안에 포인터 변수를 저장
배열 포인터 : int(*arr)[4]; -> 4칸 크기의 배열을 가리키는 포인터
'C++ > 공부 정리' 카테고리의 다른 글
연결리스트 탐색, 연결리스트로 구현한 큐(Queue)와 스택(Stack) (0) | 2021.10.22 |
---|---|
진수변환, 연결리스트 준비 (0) | 2021.10.14 |
namespace, 재귀호출(가지치기) (0) | 2021.10.05 |
path 전역 배열, n차원 배열 (0) | 2021.10.05 |
함수 오버로딩, public & private (0) | 2021.10.04 |