less than 1 minute read

2차원 배열

#include <bits/stdc++.h> 
#define ll long long
using namespace std;


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int b[2][3] = { { 2,3,6 }, { 4,5,8 } };
    int (*p)[3] = b; //2차원 배열 포인터 선언방법
    printf("%d\n", *p); //b[0][0];
    printf("%d\n", *p + 1); //b[0][1]
    printf("%d\n", *(p + 1)); //b[1][0]
    printf("%d\n", *(p + 1) + 1); //b[1][1]
    //값을 출력하는법
    printf("%d\n", *(*p)); //b[0][0];
    printf("%d\n", *(*p) + 1); //b[0][1]
    printf("%d\n", *(*(p + 1))); //b[1][0]
    printf("%d\n", *(*(p + 1) + 1)); //b[1][1]
    return 0;
}

값을 출력할때는 *(주소)를 해야합니다.
shot2
그림과 같이 열이 3개짜리 인트형 배열일때는 b[0][3]까지 12바이트를 사용합니다.
b[1][0]은 b[0][0]보다 12바이트 만큼 차이납니다.
b[i][j]의 주소는 = *(b[i] + j)
값은 * ( *(b + i) + j)

3차원 배열

#include <bits/stdc++.h> 
#define ll long long
using namespace std;


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int c[3][2][2] = 
    {
        { {2,5}, {7,9} },
        { {3,4}, {6,1} },
        { {0,8}, {11,13} }
    };
    int (*p)[2][2] = c;
    printf("%d\n", *(*(*(c)))); //2
    printf("%d\n", *(*(*(c + 1)))); //3
    printf("%d\n", *(*(*(c + 2)))); //0
    printf("%d\n", *(*(*(c) + 1))); //7
    printf("%d\n", *(*(*(c) + 1) + 1)); //9
    return 0;
}

shot3
포인터의 가장 안쪽에는 가장 기준이 되는 것이다.

Leave a comment