[3]

ucf·2020년 10월 1일
0

알고리즘&자료구조

목록 보기
4/13

1. 다차원 배열

1차원 배열

a[1]

2차원 배열

a[2][3] //2행 3열

ㅇ초기화방법

int a[2][3] = {{1,2,3},{1,2,3}};

반드시 {}를 중첩할 필요는 없다
int a[2][3] = {1,2,3,1,2,3};

ㅇ또한 초기화를 통해 요소의 개수를 알고있을 때는 맨 앞의 요소개수를 생략가능

int a[][3]={{1,2,3},{1,2,3}};

ㅇ초기화에 없는요소는 0으로 초기화된다.

3차원배열

a[3][2][1]

2. 구조체

//구조체 abc 선언
struct abc {
    int x;
    int y;
    int z;
};

//struct abc형 ex를 정의
struct abc ex;

//ex를 가르키는 포인터
struct xyz *p = &ex;

ex.x // 객체ex안의 맴버 x에 접근

p->x // p가 가르키는 객체안의 맴버 x에 접근

typedef struct abc ABC; // struct abc와 동의어인 ABC를 선언

구조체를 사용한 예제)

#include<stdio.h>

typedef struct{
        char name[3];
        int height;
        int weight;
} XYZ;
int main(){
        XYZ a = {"abc",190,70};
        printf("%s %d %d",a.name,a.height,a.weight);
}

구조체와 배열을 사용한 예제)

#include<stdio.h>

typedef struct{
        char name[3];
        int height;
        int weight;
} XYZ;
int main(){
        XYZ a[] = {{"abc",190,70},{"def",160,55}};
        printf("%s %d %d\n",a[0].name,a[0].height,a[0].weight);
        printf("%s %d %d",a[1].name,a[1].height,a[1].weight);
}
profile
즐기자!

0개의 댓글