In Data type, a data type was declared for one variable and a value was assigned. However, it is very inefficient to declare 100 variables for 100 values. Arrays
are used to manipulate multiple values like this. An array
can be understood as a set of the same data types and managed as one. To use an array, you need to declare the data type and the number of elements. In this case, use square brackets to declare element number, and use curly brackets to declare initial values.
데이터 타입을 설명할 때, Data type에서는 변수 한개에 대해 자료형을 선언하고 값을 대입했다. 하지만 100개의 값에 대해서 100개의 변수를 선언하는 것은 매우 비효율적이다. 이렇게 여러 값을 다루고자 할 때 배열
을 사용한다. 배열
은 같은 자료형을 묶어서 하나로 관리하는 집합으로 이해 할 수 있다. 배열을 사용하기 위해서는 자료형에 대한 선언과 요소 수에 대한 선언이 필요하다. 이때 요소 수에 대한 선언은 대괄호를 사용하고, 초기 값에 대한 선언은 중괄호를 사용한다.
int x[element number]={initial value, };
After declaring the array, it is possible to access each element. It is accessed using square brackets in the same way as the declaration for element number, but the element reference range starts from 0
and element number(N)-1
.
배열을 우선 선언하고 나면 각 요소에 접근할 수 있는데 요소 수에 대한 선언과 동일하게 대괄호를 사용하여 접근하지만 요소참조범위는 0
에서부터 시작하며 요소수-1
까지이다.
#include <stdio.h>
int x[5];
char y[5];
int main(void){
int a[3]={1,2,3};
int b[4]={10,};
int c[10]={0,};
char ca[3]={'a','b','c'};
char cb[3]="AB";
char cc[3]={'\0',};
int dir[]={1,0,-1,0};
return 0;
}
When declaring an array, initialization must be performed when declaring it as a local variable, and when declaring it as a global variable, all elements are automatically initialized to 0
or '\0'
. If the number of values smaller than the size of the array is declared, the remaining values are automatically assigned as 0
or '\0'
. In particular, the initialization for declaring 0 in local variables is often used. Conversely, when all the initialization values are allocated, the element number in the array can be omitted. However, it is not possible to initialize by using more elements than the size of the array. In the case of strings, the null character '\0'
character is always included at the end, so be careful when calculating the size of the array.
배열을 선언할 때, 지역변수로 선언하는 경우 초기화를 반드시 수행해주어야하며, 전역변수로 선언하는 경우 모든 요소가 0
이나 '\0'
으로 자동 초기화 된다. 초기 값을 할당할 때에 배열의 크기보다 작은 값을 선언하면 나머지 값은 0
혹은 '\0'
으로 자동으로 할당된다. 특히 지역변수에서 0으로 선언하는 구문은 자주 사용된다. 반대로 모든 초기화 값을 할당하는 경우 배열의 요소수를 생략할 수 있다. 하지만 배열의 크기보다 많은 요소를 사용하여 초기화를 할 수는 없는데, 문자열의 경우 항상 맨 마지막에 null 문자인 '\0'
문자가 포함되어 있기 때문에 배열의 크기를 계산할 때 유의해야한다.
#include <stdio.h>
int main(void){
int a[10]={0,};
for(int i=0; i<10; i++){
scanf("%d", &a[i]);
}
char b[10]={'\0',};
scanf("%s", b);
for(int i=0; i<10; i++){
printf("%d", a[i]);
}
printf("\n%s", b);
return 0;
}
1 2 3 4 5 6 7 8 9 10
test
12345678910
test
- Multidimensional array 다차원 배열
Multidimensional arrays can be declared using consecutive square brackets, and their properties are the same as one-dimensional arrays. Although the term multi-dimensional was used, the order or method in which the arrays are stored in memory is performed sequentially, and for accessing and understanding the elements of each array, this article understands the same as x[row][column]
in the case of a two-dimensional array.
대괄호를 연속적으로 사용하여 다차원 배열을 선언할 수 있으며 속성은 일차원 배열과 동일하다. 다차원이라는 용어를 사용하긴 했지만 배열이 메모리에 저장되는 순서나 방식은 순차적으로 수행되며, 각 배열의 요소에 접근 및 이해를 위해 이 글에서는 2차원 배열의 경우 x[row][column]
와 같이 이해한다.
As the dimension of the array increases and the size of the array increases, it is difficult to check element number in the array every time, so a form using the sizeof
function is often used. The element number in the first dimension is sizeof(a)/sizeof(a[0])
, and the element number in the second dimension is calculated as sizeof(a[0])/sizeof(a[0][0])
.
배열의 차원이 커지고 크기가 커지면 배열의 요소 수를 매번 확인하기 힘들기 때문에 sizeof
함수를 이용한 형태를 많이 사용한다. 첫 번째 차원의 요소 수는 sizeof(a)/sizeof(a[0])
이며, 두 번째 차원의 요소 수는 sizeof(a[0])/sizeof(a[0][0])
와 같이 계산한다.
#include <stdio.h>
int main(void){
int a[3][3]={ {1,2,3}, {1,2,3}, {1,2,3} };
for(int i=0; i<3;i++){
for(int j=0; j<3; j++){
printf("%d", a[i][j]);
}
}
for(int i=0; i < sizeof(a) / sizeof(a[0]) ; i++){
for(int j=0; j < sizeof(a[0]) / sizeof(a[0][0]) ; j++){
printf("%d", a[i][j]);
}
}
return 0;
}
123123123
123123123