hawon6691 2025. 5. 28. 18:41
728x90

배열

#include <stdio.h>

int main(void)
{
	const int index_int = 3;
	// index_1 = 1;
	printf("%d\n", index_int);

	int index_intArr[index_int] = { 1, 2, 3 };
	//index_arr = 1; error
	//index_arr[0] = 1;
	//index_arr[1] = 2;
	//index_arr[2] = 3;
	//index_arr[3] = 4; // run time error
	// 0 ~ (첨자 - 1) 까지 이므로 3은 안됨.
	// 첨자 개의 방 이므로 3이면 0, 1, 2 0으로 시작하지만
	// 방은 3개여서 첨자 까지가 아닌 첨자 개수의 방으로 알아야 한다.

	printf("%d | %d | %d \n", index_intArr[0], index_intArr[1], index_intArr[2]);

	char index_char = 'S';

	printf("%c\n", index_char);

	// char index_charArr[3] = { 'F','U','\0'};
	// \0 이 없으면 컴퓨터는 배열 끝을 모르고 임의의 값을 계속 읽어서 오류가 남.
	// 그래서 항상 마지막 방에는 \0을 넣어야 함.
	// 이게 귀찮다면
	char index_charArr[3] = "FU";
	// 이렇게 사용해도 된다.

	/*index_charArr[0] = 'F';
	index_charArr[1] = 'U';
	index_charArr[2] = 'N';*/

	printf("%c\n", index_charArr[0]);


	return 0;
}

포인터

#include <stdio.h>

int main(void)
{
	int indexInt1 = 1, indexInt2;
	// int *pointer_indexInt = 100; //error 주소가 아닌 것을 저장하면 오류. 
	int* pointerIndexInt1 = &indexInt1;  // 포인터 변수 | 주소를 가르키는 변수
										 // 여기서 붙은 *은 연산자가 아닌
										 // 구두점임.
	//indexInt2 = indexInt1;
	indexInt2 = *pointerIndexInt1; // *을 안찍으면 주소가 들어감
								   // *을 찍으면 그 주소로 가서 값을 가져옴.
								   // 그래서 indexInt1의 값이 들어감
								   // 여기서 붙은 *은 연산자임.
	// indexInt1의 주소를 *pointerIndexInt1에 넣고
	// *pointerIndexInt1의 가서 값을 가져와서 indexInt2 넣어라

	printf("indexInt1 : %d | pointerIndexInt1 : %p \n", indexInt1, &indexInt1);
	printf("indexInt2 : %d | pointerIndexInt2 : %p \n", indexInt2, &indexInt2);

	return 0;
}

포인터 _ 문자열
 

#include <stdio.h>

int main(void)
{
	int indexInt = 1;
	int* pointerIndexInt = &indexInt;
	// 변수 pointerIndexInt가 int형 데이터를 참조하기
	// 위한 포인터 변수

	// pointerIndexInt가 지시하는 곳의 자료형이 int형

	char indexChar = 'A';
	char* pointerIndexChar = &indexChar;
	// 변수 pointerIndexChar가 char형 데이터를 참조하기
	// 위한 포인터 변수

	// pointerIndexChar가 지시하는 곳의 자료형이 char형

	// 중요한 이유는 문자열 자료형이 없음.
	// Java, Python, C# 등 문자열 자료형은 있지만 
	// C언어에는 없음.
	// 하지만 다른 언어들 또한 문자열을 추상화한 객체로 제공하고
	// 내부적으로 char[] 배열, 문자형 배열을 자동 관리하고 있기에 
	// 기본적인 개념은 C언어와 같다.
	// C언어의 문자열은 문자형 배열로 저장할 수도 있지만 
	// 포인터 변수로도 저장이 가능하다.

	char indexChar1[4] = { 'F','U','N','\0' };
	char indexChar2[4] = "FUN";
	// 배열로 문자열을 다루면 하나하나다 다뤄야 하지만
	// 배열 문자열은 크기가 정해져 있음(정적이다)

	// 한글은 2 byte 씩 차지하기 때문에
	// 배열을 이용하기 보단 포인터를 이용하는 것이 좋다.

	const char* indexChar3 = "FUN"; 
     // char* indexChar3 = "FUN"; 이렇게 작성하면 "FUN"이 const char* 형태라 오류가 나서 앞에 const를 붙여야 한다.
     // char* indexChar3; 이렇게 작성하면 초기화가 안되있다고 NULL로 초기화해도 오류가 나서 (char*)malloc(20 * sizeof(char))로 초기화 해주어야 한다. 사용 후 반드시 free(포인터 변수명)으로 메모리를 해제해야 합니다.
	// 항상 첫 번째 방 주소만 다룸. 
	// 4 byte 커봐야 8 byte 임
	// 포인터 문자열은 크기가 정해져 있지 않음(동적이다)
	// 첫 번째 주소만 가져왔기 때문에
	// 얼마든지 크기를 변경할 수 있다.
	// 문자열을 다 다루는 것보다  
	// 첫 번째 주소만 다루면 속도가 훨씬 더 빠르다.
	// 함수에 넘길 때도 속도가 빠르다.
	// 일반적으로 포인터를 이용하게 되면 성능이 많이 좋아짐.
	// C, C++이 아직도 사용하는 이유는 포인터가 있기 때문.

	// F가 저장된 곳의 주소가 들어가 있음.
	// 그 주소로 가면 F가 있고 그 다음 주소로 가면 U가 있음.
	// 이러한 형태를 문자열이라고 함.
	


	return 0;
}

구조체

#include <stdio.h>

// 구조체를 이용하면 내가 원하는 자료형을
// 만들어서 사용할 수 있음.

// 자동차 엔진 정보 구조체
struct Engine {         // 구조체 선언 
    const char* type;      // 멤버 | 엔진 타입
    int horsepower;     // 멤버 | 마력
    float displacement; // 멤버 | 배기량
};

struct Car {
    const char* model;     // 모델명 
    const char* manufacturer; // 제조사
    int year;           // 연식
    struct Engine engine;      // 엔진 정보
    int mileage;        // 주행 거리
    float fuelLevel;    // 연료 잔량
};

int main(void)
{
    struct Car myCar1;

    //myCar1 = 10; //error Car 안에 여러가지 자료형들이 있어서 안됨

    myCar1.model = "Model S"; // 구조체 변수의 멤버 model에 접근(점 사용)
    myCar1.manufacturer = "Tesla";
    myCar1.year = 2025;
    myCar1.engine.type = "전기";
    myCar1.engine.horsepower = 670;
    myCar1.engine.displacement = 0.0;
    myCar1.mileage = 15000;
    myCar1.fuelLevel = 50.0;

    struct Car myCar2 = { // struct Car 구조체형 변수 myCar 선언
        "Model S",
        "Tesla",
        2025,
        {"전기", 670, 0.0},
        15000,
        50.0
    }; 

    struct Car myCar3, myCar4 = { // struct Car 구조체형 변수 myCar 선언
        "Model S",
        "Tesla",
        2025,
        {"전기", 670, 0.0},
        15000,
        50.0
    }; // error myCar3에 초기화 된 값이 없기 때문에

    myCar3 = myCar4;
    // 값을 넣주면 됨.

    printf("모델: %s\n", myCar1.model);
    printf("제조사: %s\n", myCar1.manufacturer);
    printf("연식: %d\n", myCar1.year);
    printf("엔진 타입: %s\n", myCar1.engine.type);
    printf("마력: %d hp\n", myCar1.engine.horsepower);
    printf("배기량: %.1f L\n", myCar1.engine.displacement);
    printf("주행 거리: %d km\n", myCar1.mileage);
    printf("연료 잔량: %.1f L\n", myCar1.fuelLevel);

    printf("모델: %s\n", myCar2.model);
    printf("제조사: %s\n", myCar2.manufacturer);
    printf("연식: %d\n", myCar2.year);
    printf("엔진 타입: %s\n", myCar2.engine.type);
    printf("마력: %d hp\n", myCar2.engine.horsepower);
    printf("배기량: %.1f L\n", myCar2.engine.displacement);
    printf("주행 거리: %d km\n", myCar2.mileage);
    printf("연료 잔량: %.1f L\n", myCar2.fuelLevel);

	return 0;
}
728x90