본문 바로가기
비트 단기/c

11/2

by woohyun22 2018. 11. 2.

 

#include "pch.h"
#include <iostream>
#pragma pack(1)//4의 배수로 안보내고 그대로 올려줌
int main()
{
 /*
 //ex1
 short *a = new short;//사용 한개
 delete a;//해제

 short*b = new short[10];//여러개 사용
 delete[] a;//해제
 */
 //ex2
 /*
 short a[10];
 short *b;//주소 받기전이라 사용불가
 b = a;
 for (int i = 0; i < 10; i++)
 {
  b[i] = i;
  printf("%d %d\n", a[i],b[i]);
 }
 delete []b;//b가 a의 주소를 삭제할 수도 있다.
 //남에주소 팔아먹는 일은 비일비재하다
 */
 /*
 short *a[10] = {0};
 for (int i = 0; i < 10; i++) {
  printf("%d %d\n", a+i,*(a+i));
 }
 printf("\n\n");
 for (int i = 0; i < 10; i++) {
  printf("%d %d\n", &a[i], a[i]);//주소를 표시하려면 앞에 &를 붙인다, 값은 그냥 a[i]
 }
 printf("\n\n");

 short ar[5] = {0};
 for (int i = 0; i < 5; i++) {
  printf("%d %d\n", &ar[i], ar[i]);
 }

 //   주소   값
 //배열  &ar[i]  ar[i]
 //그냥 ar+i   *(ar+i)
 short ar[5] = { 0 };
 for (int i = 0; i<5; i++) {
  printf("%d %d\n", ar + i, *(ar + i));
 }
 */
 //속성
 /*
 //ex1
 short AppleColor=10;//카멜표기법
 short ApplePrice = 20;
 short AppleWeight = 30;
 short BananaColor = 40;
 short BananaPrice = 50;
 short BananaWeight = 60;
 short OrangeColor = 70;
 short OrangePrice = 80;
 short OrangeWeight = 90;
 */
 //속성을 모아놓은 박스 = 구조체
 //구조체 이름은 전부 대문자로 하자!
 //스코프는 자바처럼 바로 붙여쓰자!
 /*
 struct  FRUIT{
  char color;
  short price;
  long weight;
 };
 FRUIT apple,banana,orange;
 apple.color=10;
 apple.price=20;
 apple.weight = 30;
 banana.price = 40;
 orange.weight = 30;

 printf("%d %d %d", apple.color, apple.price, apple.weight);
 */
 /*
 struct  FRUIT {
  char color;
  short price;
  long weight;
 };
 //구조체 선언과 동시에 초기화
 FRUIT apple = {10,20,30};//순서대로 값이 들어가
 FRUIT banana = { 40,50 };//넘치면 x
 FRUIT orange = { 70,80,90};//   - FRUIT type이다!, 대입연산자는 쌍방간의 type이 같아야한다.

 apple.color = 10;
 apple.price = 20;
 apple.weight = 30;
 banana.price = 40;
 orange.weight = 30;
 printf("%d %d %d\n", apple.color, apple.price, apple.weight);
 printf("%d %d", sizeof(long), sizeof(FRUIT));//FRUIT 의 사이즈의 합이 7이지만 컴퓨터는 짝수로 준다 특히 4의 배수

 //진짜 구조체 사이즈를 알려달라는 옵션
 */
 //

 /*
 struct  FRUIT {//구조체 이름은 중복될수없다.
  char color;
  short price;
  long weight;
 };
 FRUIT apple = { 10,20,30 };
 FRUIT banana;
 long orange;
 banana = apple;//속성이 대입됨
 printf("%d %d %d\n", banana.color, banana.price, banana.weight);//type이 같아서 가능

 //orange = apple;//type이 다르다 FRUIT와 long
 FRUIT a = { 10,20,30 };
 FRUIT *b;//공유 or할당되야 사용가능
 b = &a;//연산자 우선순위 (*b)
 printf("%d\n", (*b).color);
 printf("%d\n", b->color);//같은 문장이나 밑의 문장이 선호된다.
 //b->color;//값넣기
 printf("%d\n", &b->color);//&은 맨뒤의 변수의 &이다.
 */
 struct  FRUIT {
  char color;
  short price;
  long weight;
 };
 FRUIT a = {10,20,30};
 FRUIT *b;
 b = &a;
 b->color=40;
 b->price = 50;
 b->weight = 60;
 a.color;
 a.price;
 a.weight;
 printf("%d %d %d\n", b->color,b->price,b->weight);
 printf("%d %d %d\n", a.color,a.price,a.weight);
 printf("\n\n");
 printf("%d %d %d\n", &b->color, &b->price, &b->weight);
 printf("%d %d %d\n", &a.color, &a.price, &a.weight);

 FRUIT *c;
 c = new FRUIT;//type신경쓰기
 c->color =10;
 c->price = 20;
 c->weight = 30;
 printf("%d %d", &c->color, c->color);
 //c를 받은 그대로
 delete c;//1개
 delete []c;//여러개
 //메모리 쓰고 또써도됨~
 c = new FRUIT;
 c->color = 100;
 char *pp;//
 pp = &c->color;//값
 printf("%d %d\n", *pp, c->color);//값
 printf("%d %d\n", pp, &c->color);//주소
 
 delete pp;
 delete c;

}

728x90

'비트 단기 > c' 카테고리의 다른 글

11/1 복습 포인터 보충1  (0) 2018.11.05
10/30 복습 포인터부터  (0) 2018.11.05
11/1  (0) 2018.11.01
10/30 pointer  (0) 2018.10.31
while, scanf,switch  (0) 2018.10.26

댓글