#include "pch.h"
#include <iostream>
struct APPLE {
short *color;
};
int main()
{
//apple이 color제어
APPLE *apple;
apple = new APPLE;//메모리 생성
apple->color = new short;//메모리 생성
*apple->color = 10;
printf("%d %d\n", apple->color, *apple->color);//주소, 값
delete apple->color;//
apple = new APPLE;
apple->color = new short[3];//
apple->color[0] = 20;
apple->color[1] = 30;
apple->color[2] = 40;//
printf("%d %d %d %d\n", apple->color, apple->color[0], apple->color[1], apple->color[2]);
delete[] apple->color;
delete apple;
//3 1//2 3//color가 apple
apple = new APPLE[3];
apple[0].color = new short;
apple[1].color = new short;
apple[2].color = new short;
*apple[0].color = 10;
*apple[1].color = 10;
*apple[2].color = 10;
printf("%d %d %d %d %d %d %d \n", apple->color, *apple[0].color, *apple[1].color, *apple[2].color, &apple[0].color, &apple[1].color, &apple[2].color);
delete apple[0].color;
delete apple[1].color;
delete apple[2].color;
delete[] apple;
apple = new APPLE[2];
apple[0].color = new short[3];
apple[1].color = new short[5];
apple[0].color[0] = 10;
apple[0].color[1] = 20;
apple[0].color[2] = 30;
apple[1].color[0] = 40;
apple[1].color[1] = 50;
apple[1].color[2] = 60;
printf("%d %d %d %d %d %d %d\n", apple, apple[0].color[0], apple[0].color[1], apple[0].color[2], apple[1].color[0], apple[1].color[1], apple[1].color[2]);
printf("%d %d %d %d %d %d %d\n", apple, &apple[0].color[0], &apple[0].color[1], &apple[0].color[2], &apple[1].color[0], &apple[1].color[1], &apple[1].color[2]);
delete[] apple[0].color;
delete[] apple[1].color;
delete[] apple;
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include "pch.h"
#include <iostream>
struct APPLE{
long *c;
};
struct BANANA {
//short a;
//long b[10];
//char *c;
APPLE *a;
};
struct ORANGE {
//short a;
//long b[10];
//char *c;
BANANA *b;//다른 구조체 사용가능
};
int main()
{ /*
ORANGE orange;
orange.banana.apple.color = 10;
//orange.banana.apple = 10;//type에 맞게 줘야된다.
printf("%d\n", orange.banana.apple.color);//값
printf("%d\n", &orange.banana.apple.color);//주소
*/
//1113//1311
/*
ORANGE *o;
o = new ORANGE;
o->b = new BANANA;
o->b->a = new APPLE;
o->b->a->c = new long[3];//3개할당
o->b->a->c[0] = 10;
o->b->a->c[1] = 20;
o->b->a->c[2] = 30;
printf("%d %d\n", o->b->a->c[0], &o->b->a->c[0]);
printf("%d %d\n", o->b->a->c[1], &o->b->a->c[1]);
printf("%d %d\n", o->b->a->c[2], &o->b->a->c[2]);
//메모리 해제할때 c를 해제해버리면 다른거 해제못함(순서대로)
delete[] o->b->a->c;
delete o->b->a;
delete o->b;
delete o;
*/
ORANGE *o;
o = new ORANGE;
o->b = new BANANA[3];
o->b[0].a = new APPLE;
o->b[1].a = new APPLE;
o->b[2].a = new APPLE;
o->b[0].a->c = new long;
o->b[1].a->c = new long;
o->b[2].a->c = new long;
*o->b[0].a->c = 10;// *을 앞으로 c []에대한 - *붙이면 값
*o->b[1].a->c = 20;
*o->b[2].a->c = 30;
printf("%d %d\n", *o->b[0].a->c, &o->b[0].a->c);
printf("%d %d\n", *o->b[1].a->c, &o->b[1].a->c);
printf("%d %d\n", *o->b[2].a->c, &o->b[2].a->c);
delete[] o->b[0].a->c;
delete[] o->b[1].a->c;
delete[] o->b[2].a->c;
delete o->b[0].a;
delete o->b[1].a;
delete o->b[2].a;
delete[] o->b;
delete o;
}
//3133
//o[].b->a[]->c[]
//3111
//o[].b->a->c;
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
'비트 단기 > c' 카테고리의 다른 글
11/07 함수 시작 (0) | 2018.11.08 |
---|---|
구조체 복습 11/06 (0) | 2018.11.07 |
11/5 포인터 구조체 (0) | 2018.11.05 |
11/2 구조체 복습 앞부분 확실히 하기 (0) | 2018.11.05 |
11/1 복습 포인터 보충1 (0) | 2018.11.05 |
댓글