12차시 - jeongpaljack/Cstudy GitHub Wiki

CRUD

  • CRUD(create, read, update, delete)의 약자
  • 데이터를 다루는 기본적인 4가지 동작

동적 메모리 할당

  • 메모리를 절약하기 위하여 사용하는 방식
  • 배열의 크기가 정해지지 않았을 경우에 사용
  • heap area에 저장됨
  • 정적 메모리 할당은 stack area에 저장되고, 함수가 끝나면 안의 정적 메모리 할당은 풀림
  • 정적 데이터 영역은 Global Data 영역

header file (헤더 제작)

  • Main.cpp를 클라이언트 파일로 하기 위해 필요함
  • 파일이름.h 라는 소스파일을 만들고, 안에 사용자 정의 함수명만 작성
  • Int.cpp 등 분류에 따라 Main에 사용할 사용자 정의 함수를 넣을 하위 파일 작성
  • Main.cpp에서는 사용자 정의 함수만 불러오도록 작성
  • Main 파일에서는 #include "헤드이름"으로 실행(중속성이 아님)

구조체

  • 서로 다른 데이터 구조를 하나의 변수로 묶어서 사용할 수 있는 사용자 정의 데이터 타입
  • float, int형을 하나로 묶을 수 있음
  • &가 ->보다 우선순위가 낮고, *가 .보다 우선순위가 낮음

당일 작성한 코드

#include <stdio.h>
#include <stdlib.h>

struct Point {

	int x;
	int y;
};

void PrintPoint(Point* p) {
	printf("(%d, %d)\n", p -> x, p -> y);
}

int main() {
	Point pt = { 2,3 };
	Point pt2 = { -1, 3 };
	Point* p = &pt;

	PrintPoint(&pt);
	PrintPoint(&pt2);


}


//#include <stdio.h>
//#include <stdlib.h>
//
//struct Point {
//
//	int x;
//	int y;
//};
//
//void PrintPoint(Point pt) {
//	printf("(%d, %d)\n", pt.x, (&pt)->y);
//}
//
//int main() {
//	Point pt = { 2,3 };
//	Point pt2 = { -1, 3 };
//	Point* p = &pt;
//
//	PrintPoint(pt);
//	PrintPoint(pt2);
//
//
//}


//#include <stdio.h>
//#include <stdlib.h>
//
//struct Point {
//	
//	int x;
//	int y;
//};
//
//void PrintPoint(Point pt) {
//	printf("(%d, %d)\n", pt.x, (&pt)->y);
//}
//
//int main() {
//	Point pt = { 2,3 };
//	Point* p = &pt;
//
//	PrintPoint(pt);
//	printf("(%d, %d)\n", (*p).x, p->y);
//	//printf("(%d, %d)\n", *p.x, p->y);		// 에러 발생: "."연산자보다 우선순위가 낮아 참조불가
//
//}


//#include <stdio.h>
//#include <stdlib.h>
//
//struct Point {
//	int x;
//	int y;
//};
//
//int main() {
//	Point pt = { 2,3 };
//	Point* p = &pt;
//
//	printf("(%d, %d)\n", pt.x, (& pt)->y);
//	printf("(%d, %d)\n", (*p).x, p->y);
//	//printf("(%d, %d)\n", *p.x, p->y);		// 에러 발생: "."연산자보다 우선순위가 낮아 참조불가
//
//}


//#include <stdio.h>
//#include <stdlib.h>
//
//struct Point {
//	int x;
//	int y;
//};
//
//int main() {
//	Point pt = { 2,3 };
//	Point* p = &pt;
//
//	printf("(%d, %d)\n", pt.x, pt.y);
//	printf("(%d, %d)\n", p->x, p->y);
//
//}



//#include <stdio.h>
//#include <stdlib.h>
//
//struct Point {
//	int x;
//	int y;
//};
//
//int main() {
//	Point pt = { 2,3 };
//	printf("(%d, %d)\n", pt.x, pt.y);
//
//	printf("%p %\p\n", &pt, &pt + 1);		// pt는 8바이트의 변수
//	printf("%p\n", &pt.x);
//	printf("%p\n", &pt.y);
//	Point* p1 = &pt;
//	int* p2 = &pt.x;
//
//
//}



//#include <stdio.h>
//#include <stdlib.h>
//
//struct Point {
//	int x;
//	int y;
//};
//
//int main() {
//	int n = 10;
//	Point pt = { 2,3 };
//
//	printf("%d\n", n);
//	printf("(%d, %d)\n", pt.x, pt.y);
//}


//#include <stdio.h>
//#include <stdlib.h>
//
//void AssignAddress(int** pp, int* pdata) {
//	*pp = pdata;
//}
//
//int main() {
//
//
//	int a = 10;
//	int b = 20;
//	int* p = NULL;
//	AssignAddress(&p, &b);
//
//	printf("data : %d\n", *p);
//}


//#include <stdio.h>
//#include <stdlib.h>
//
//void AllocIntArray(int** pa, int capacity) {
//	*pa = (int*)malloc(sizeof(int) * capacity);
//}
//
//void InitIntArray(int* pa, int size) {
//	for (int i = 0; i < size; ++i)
//		pa[i] = i + 1;
//}
//
//void PrintIntArray(int* pa, int size) {
//	for (int i = 0; i < size; ++i)
//		printf("data : %d\n", pa[i]);
//}
//
//void FreeIntArray(int* pa) {
//	free(pa);
//}
//int main() {
//
//
//	int* pa = NULL;
//
//	InitIntArray(pa, 10);
//	PrintIntArray(pa, 10);
//	FreeIntArray(pa);
//}



//#include <stdio.h>
//#include <stdlib.h>
//
//int* AllocIntArray(int capacity) {
//	return (int*)malloc(sizeof(int) * capacity);
//}
//
//void InitIntArray(int* pa, int size) {
//	for (int i = 0; i < size; ++i)
//		pa[i] = i + 1;
//}
//
//void PrintIntArray(int* pa, int size) {
//	for (int i = 0; i < size; ++i)
//		printf("data : %d\n", pa[i]);
//}
//
//void FreeIntArray(int* pa) {
//	free(pa);
//}
//int main() {
//
//
//	int* pa = AllocIntArray(10);
//
//	InitIntArray(pa, 10);
//	PrintIntArray(pa, 10);
//	FreeIntArray(pa);
//}

//#include <stdio.h>
//#include <stdlib.h>
//
//int* AllocInt() {
//	return (int*)malloc(sizeof(int));
//}
//double* AllocDouble() {
//	return (double*)malloc(sizeof(double));
//}
//
//void SetInt(int* p, int data) {
//	*p = data;
//}
//
//void SetDouble(double* p, double data) {
//	*p = data;
//}
//
//void PrintInt(int data) {
//	printf("int : %d\n", data);
//
//}
//
//void PrintDouble(double data) {
//	printf("double : %g\n", data);
//
//}
//
//void FreeInt(int* data) {
//	free(data);
//}
//void FreeDouble(double* data) {
//	free(data);
//}
//
//int main() {
//	int* pa = NULL;
//	double* pb = NULL;
//	// CRUD(create, read, update, delete)
//	pa = AllocInt();
//	pb = AllocDouble();
//	SetInt(pa, 10);
//	SetDouble(pb, 3.14);
//	PrintInt(*pa);
//	PrintDouble(*pb);
//	FreeInt(pa);
//	FreeDouble(pb);
//
//
//}


//#include <stdio.h>
//#include <stdlib.h>
//
//int main() {
//	int* pa = NULL;
//	double* pb = NULL;
//	pa = (int*)malloc(sizeof(int));
//	pb = (double*)malloc(sizeof(double));
//	*pa = 10;
//	*pb = 3.14;
//	printf("int : %d\n", *pa);
//	printf("double : %g\n", *pb);
//	free(pa);
//	free(pb);
//
//}
⚠️ **GitHub.com Fallback** ⚠️