본문 바로가기

Programming/C++

C++ 배열의 기초

#include <iostream>

using namespace std;

int main()
{
    int one_student_score; // 1 variable
    int student_scores[5]; // 5 int

    // array의 각각 element는 각각의 변수로 사용할 수 있다.

    return 0;
}

struct를 이용하여 array를 사용하는 방법

 

#include <iostream>

using namespace std;

struct Rectangle
{
    int length;
    int width;
};

int main()
{
    cout << sizeof(Rectangle) << endl;

    Rectangle rect_arr[10];

    //각각의 element를 사용한다면
    rect_arr[0].length = 1;
    rect_arr[0].width = 2;

    cout << sizeof(rect_arr) << endl;
    return 0;
}

 


array를 초기화 하는 방법

 

#include <iostream>

using namespace std;

enum StudentName
{
    JACKJACK, // = 0
    DASH,     // = 1
    VIOLET,   // = 2
};

int main()
{
    int my_array[5] = {
        1,
        2,
    };

    cout << my_array[0] << endl;
    cout << my_array[1] << endl;
    cout << my_array[2] << endl;
    cout << my_array[3] << endl;
    cout << my_array[4] << endl;

    //초기화 할 갯수가 확실하다면 영역할당을 확실히 안해주어도 괜찮다.
    int m_array[] = {1, 2, 3, 4, 5};

    cout << m_array[0] << endl;
    cout << m_array[1] << endl;
    cout << m_array[2] << endl;
    cout << m_array[3] << endl;
    cout << m_array[4] << endl;

    // 변수 번호를 enum을 통해서도 가능하다.

    cout << m_array[JACKJACK] << endl;
    cout << m_array[DASH] << endl;
    cout << m_array[VIOLET] << endl;
    cout << m_array[3] << endl;
    cout << m_array[4] << endl;

    return 0;
}

안들어간 부분은 0으로 초기화가 된 부분이다.

 

#include <iostream>

using namespace std;

int main()
{
    int num_students = 0;
    cin >> num_students;

    int students_scores[num_students];

    return 0;
}

#include <iostream>

using namespace std;

//c 언어에서는 메크로를 사용하기도 함
//c++에서는 동적할당을 이용하서 사용한다. 
#define NUM_STUDENTS 100000

int main()
{
    int num_students = 0;
    cin >> num_students;

    int students_scores[num_students];

    return 0;
}

동적 할당을 할때는 이렇게 사용한다.


#include <iostream>

using namespace std;

#define NUM_STUDENTS 100000

void doSomething(int students_scores[20])
{
    cout << (size_t)students_scores << endl;
    cout << students_scores[0] << endl;
    cout << students_scores[1] << endl;
    cout << students_scores[2] << endl;
    cout << "Size of doSomething " << sizeof(students_scores) << endl;
}

int main()
{
    const int num_students = 20;
    //cin >> num_students;

    int students_scores[num_students] = {1, 2, 3, 4, 5};

    //주소의 위치를 10진수로 나타낼때는 int 말고 size_t로 사용한다.
    cout << (size_t)students_scores << endl;
    cout << students_scores[0] << endl;
    cout << students_scores[1] << endl;
    cout << students_scores[2] << endl;
    cout << "Size of main " << sizeof(students_scores) << endl;

    doSomething(students_scores);

    return 0;
}

함수를 넣어서 똑같이 사용시킬 수도 있다.

c 언어의 기억을 되돌리면 배열은 함수로 보낼때도 주소가 그대로 간다. 

 

결과값을 보면 main에서는 array로 사이즈가 할당이 되지만

함수 doSomething으로 넘어갈때는 포인터로 넘어갔다.

'Programming > C++' 카테고리의 다른 글

C++ 난수 random number 만들기  (0) 2021.03.14
C++ 배열의 반복  (0) 2021.03.14
C++ Fundamental data Types, Integers, Float, Char type  (0) 2021.02.09
C++ function  (0) 2021.02.09
C++ 공부  (0) 2021.02.08