https://youtu.be/osL_ngXRmA4

https://youtu.be/UV30FZqQsAM

https://youtu.be/DH_sIesP0zs

main.cpp

#include <iostream>
#include "Arr.h"

int main()
{
	tArr s1 = {};

	InitArr(&s1);

	for (int i = 0; i < 10; ++i)
	{
		PushBack(&s1, i);
	}

	for (int i = 0; i < s1.iCount; ++i)
	{
		printf("%d\n",s1.pInt[i]);
	}
	ReleaseArr(&s1);

	return 0;
}

Arr.h

#pragma once

//ints
typedef struct _tagArr
{
	int*		pInt; // 포인터
	int			iCount; // 카운터
	int			iMaxCount; // 최대 카운터
}tArr;

// 배열 초기화 함수
void InitArr(tArr* _pArr);


//데이터 추가 함수
void PushBack(tArr* _pArr, int _iData);

//공간 추가 확장
void Reallocate(tArr* _pArr);

//배열 메모리 해제 함수
void ReleaseArr(tArr* _pArr);

Arr.cpp

#include "Arr.h"
#include <iostream>

//초기화
void InitArr(tArr* _pArr)
{
	_pArr->pInt = (int*)malloc(sizeof(int) * 2);
	_pArr->iCount = 0;
	_pArr->iMaxCount = 2;
}

void Reallocate(tArr* _pArr)
{
	//1. 2배 더 큰 공간을 동적할당한다.
	int* pNew = (int*)malloc(_pArr->iMaxCount * 2 * sizeof(int));

	//2. 기존 공간에 있던 데이터들을 새로 할당한 공간으로 복사시킨다.
	for (int i = 0; i < _pArr->iCount; ++i)
	{
		pNew[i] = _pArr->pInt[i];
	}
	//3. 기존 공간은 메모리 해제
	free(_pArr->pInt);

	//4. 배열이 새로 할당된 공간을 가리키게 하다.
	_pArr->pInt = pNew;

	//5. MaxCount 변경점 적용
	_pArr->iMaxCount *= 2;
	
}

void PushBack(tArr* _pArr, int _iData)
{
	// 힙 영역에 할당한 공간이 다 참 확인
	if (_pArr->iMaxCount <= _pArr->iCount)
	{
		//재할당
		Reallocate(_pArr);
	}

	// 데이터 추가
	_pArr->pInt[_pArr->iCount++] = _iData;
	
}

// 힙 영역 할당 제거
void ReleaseArr(tArr* _pArr)
{
	free(_pArr->pInt);
	_pArr->iCount = 0;
	_pArr->iMaxCount = 0;

}

 

 

두가지 방법은 동일합니다.  포인트를 선택 할 수 있습니다.

_pArr->iCount = 0;
(*_pArr).iCount = 0;

구조체 포인트 선택

 

 

Arr.cpp (정렬) 추가

https://youtu.be/FiqrUZHpAOQ

#include "Arr.h"
#include <iostream>

//초기화
void InitArr(tArr* _pArr)
{
	_pArr->pInt = (int*)malloc(sizeof(int) * 2);
	//(*_pArr).iCount = 0;
	_pArr->iCount = 0;
	_pArr->iMaxCount = 2;
}

void Reallocate(tArr* _pArr)
{
	//1. 2배 더 큰 공간을 동적할당한다.
	int* pNew = (int*)malloc(_pArr->iMaxCount * 2 * sizeof(int));

	//2. 기존 공간에 있던 데이터들을 새로 할당한 공간으로 복사시킨다.
	for (int i = 0; i < _pArr->iCount; ++i)
	{
		pNew[i] = _pArr->pInt[i];
	}
	//3. 기존 공간은 메모리 해제
	free(_pArr->pInt);

	//4. 배열이 새로 할당된 공간을 가리키게 하다.
	_pArr->pInt = pNew;

	//5. MaxCount 변경점 적용
	_pArr->iMaxCount *= 2;
	
}

void PushBack(tArr* _pArr, int _iData)
{
	// 힙 영역에 할당한 공간이 다 참 확인
	if (_pArr->iMaxCount <= _pArr->iCount)
	{
		//재할당
		Reallocate(_pArr);
	}

	// 데이터 추가
	_pArr->pInt[_pArr->iCount++] = _iData;
	
}

// 힙 영역 할당 제거
void ReleaseArr(tArr* _pArr)
{
	free(_pArr->pInt);
	_pArr->iCount = 0;
	_pArr->iMaxCount = 0;
}

void Sort(tArr* _pArr)
{
	//데이타가 1개 이하인
	if (_pArr->iCount <= 1)
		return;

	//오름차순 정렬

	while (true)
	{
		bool bFinish = true;
		int iLoop = _pArr->iCount - 1;
		for (int i = 0; i < iLoop; ++i)
		{
			if (_pArr->pInt[i] > _pArr->pInt[i + 1])
			{
				int iTemp = _pArr->pInt[i];
				_pArr->pInt[i] = _pArr->pInt[i + 1];
				_pArr->pInt[i + 1] = iTemp;
				bFinish = false;
			}
		}

		if (bFinish)
			break;
	}

}

 

'C,C++' 카테고리의 다른 글

C,C++ 템플릿  (0) 2023.07.17
C,C++ 기초 CLASS / STRUCT  (0) 2023.07.16
C,C++기초 리스트  (0) 2023.07.15
C,C++기초 함수 포인터  (0) 2023.07.15
C언어의 기초 문법  (0) 2023.07.12

구조체의 뜻과 종류; struct와 typedef struct

 

 구조체는 직접 변수의 형태를 만들 수 있는 문법을 말합니다. 구조체는 2가지 종류가 있는데 그것은 struct와 typedef struct입니다.

struct student_info{
    int number;
    char name[20];
    int age;
}
 
int main(){
    struct student_info s = {1, "Hong gill dong", 12};
    s.number = 10;
}
 

이건 struct 구문의 예시입니다. struct는 조금 불편하게 main함수에서 변수를 선언할 때 struct를 써줘야 합니다. struct 구문 안에 보시면 변수들이 있습니다. 그러니까 student_info 형태의 변수는 이 3개의 변수를 한 번에 내장하고 있다는 것입니다. 그래서 이 변수의 값을 바꿀 때도 전체 변수 s 안에 있는 변수 중 number이 10이라고 정하려면 s.number = 10;이라고 써줘야 합니다.


자 이제 typedef struct를 알아보겠습니다.

typedef struct {
    int num;
    char grade;
}student;
 
int main(){
    student s = {1, 'A'};
}​

여기에서 typedef struct와 struct 구문의 차이점이 나옵니다. typedef struct는 struct를 쓰지 않고 그냥 형태만 쓰면 되거든요. 대신 typedef struct는 조금 다른 점이 형태가 중괄호 마지막 부분에 나옵니다. 이 점 꼭 아셔야 합니다^^

출처 : https://opentutorials.org/module/5371/30564

 

구조체의 뜻과 종류; struct와 typedef struct - C언어의 기초 문법

 구조체는 직접 변수의 형태를 만들 수 있는 문법을 말합니다. 구조체는 2가지 종류가 있는데 그것은 struct와 typedef struct입니다. struct student_info{ int number; char name[20]; int age; } int main(){ struct student_

opentutorials.org

 

struct

구조체란? 구조체(structure type)란 사용자가 C언어의 기본 타입을 가지고 새롭게 정의할 수 있는 사용자 정의 타입입니다. 구조체는 기본 타입만으로는 나타낼 수 없는 복잡한 데이터를 표현할 수 있습니다.

 

typedef

typedef는 기존의 존재하는 자료형에(구조체) 새 이름(별명)을 부여하는 것입니다. 정수 자료형 int를 다른 이름으로 설정할 수 있다는 뜻입니다. typedef int InT; int의 또 다른 이름 InT가 됩니다.

 

static
모든 객체가 한 메모리를 공유하는 멤버 변수. 객체 별로 각각 할당되는 멤버가 아니라 모든 객체가 공유하는 멤버다.

 

https://www.youtube.com/watch?v=Nrtg_YSqwu4&list=PL4SIC1d_ab-aOxWPucn31NHkQvNPHK1D1&index=22&ab_channel=%EC%96%B4%EC%86%8C%ED%8A%B8%EB%9D%BD%EA%B2%8C%EC%9E%84%EC%95%84%EC%B9%B4%EB%8D%B0%EB%AF%B8 

sizeof

 

'C,C++' 카테고리의 다른 글

C,C++ 템플릿  (0) 2023.07.17
C,C++ 기초 CLASS / STRUCT  (0) 2023.07.16
C,C++기초 리스트  (0) 2023.07.15
C,C++기초 함수 포인터  (0) 2023.07.15
C 기초 가변 배열  (0) 2023.07.14

+ Recent posts