기본 콘텐츠로 건너뛰기

배열

배열은 동일한 자료형으로 구성된 기억 장소의 집합이다. 배열 내의 특정 위치를 지칭하기 위해서 첨자를 사용한다. 배열의 첨자는 대괄호 [ ]로 표현한다. 배열의 요소는 항상 0부터 시작하며, 마지막 요소의 번호는 n-1이 된다. 배열명은 배열의 시작 주소이다.
 1 자료형 배열명[갯수];



다차원 배열

2차원 배열은 2개의 첨자를 가지며, 3차원 배열은 3개의 첨자를 가진다. 배열의 차원에는 제한이 없다.
2차원 배열의 요소는 1차원 배열이다. 2차원 배열에서 행의 숫자는 생략할 수 있다.

 1 자료형 배열명[갯수][갯수];



초기화

 1 int arr[4] = {10, 20, 30, 40};
 2 int arr[] = {10, 20, 30, 40};
 3 int arr[4][2] = {10, 20, 30, 40, 50, 60, 70, 80};
 4 int arr[4][2] = {{10, 20}, {30, 40}, {50, 60}, {70, 80}};
 5 int arr[4] = {10, 20, 30, }; // 쉼표를 사용하면 마지막 요소는 0으로 초기화된다.



배열의 크기

 1 #include <stdio.h>
 2 
 3 /*
 4     배열의 크기
 5 */
 6 
 7 int main ()
 8 {  
 9     int arr[] = {1, 2, 3, 4};
10 
11     printf("%d\n", sizeof(arr)/sizeof(int));
12 
13     system("PAUSE");
14     return 0;
15 }




문자의 배열

문자열을 저장하기 위해서는 문자형배열을 이용할 수도 있고, 포인터를 이용할 수도 있다. 배열의 이름은 포인터의 역할을 하므로, 두 가지는 실제로 같은 것이라 볼 수 있다.
문자열의 마지막에는 널문자가 포함되야 하므로, 문자형 배열의 크기는 실제 문자의 갯수보다 1이 커야한다.
 1 char str[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
 2 char str[6] = "Hello";
 3 char* str = "Hello";

댓글

이 블로그의 인기 게시물

C 표준, GCC 그리고 컴파일 옵션

GCC supports three versions of the C standard, although support for the most recent version is not yet complete. The original ANSI C standard (X3.159-1989) was ratified in 1989 and published in 1990. This standard was ratified as an ISO standard (ISO/IEC 9899:1990) later in 1990. There were no technical differences between these publications, although the sections of the ANSI standard were renumbered and became clauses in the ISO standard. This standard, in both its forms, is commonly known as  C89 , or occasionally as  C90 , from the dates of ratification. The ANSI standard, but not the ISO standard, also came with a Rationale document. To select this standard in GCC, use one of the options  -ansi ,  -std=c90  or  -std=iso9899:1990 ; to obtain all the diagnostics required by the standard, you should also specify  -pedantic  (or  -pedantic-errors  if you want them to be errors rather than warnings). See  Options Controlling C Dialect . Errors in the 1990 ISO C standard were cor

연산자 우선 순위

() (괄호) [] (배열) -> (포인터멤버) . (구조체멤버) ! (부정) ~ (1의보수) ++ (증가) -- (감소) - (부호)  + (부호)  (자료형) (형변환)  * (포인터)  & (주소)  sizeof * (곱셈) / (나눗셈) % (나머지) + (덧셈) - (뺄셈) << >> (비트이동) < <= > >= (관계) == != (관계) & (비트논리곱) ^ (비트배타논리합) | ( 비트논리합 ) && (논리곱) || (논리합) ?: (조건) = (할당) += -= *= /= %= &= ^= |= <<= >>= (복합할당) , (쉼표)

자료형

자료형이란 데이터나 자료의 종류와 형태를 말한다. 자료형은 정밀도에 따라, 부호 유무에 따라서 세부적으로 구분된다. 기본형 정수형 int, short, long, unsigned 소숫점 아래에 어떤 값을 갖지 않는 숫자를 정수라고 한다. 실수형 float, double 아주 크거나 아주 작은 값을 표현하기 위해 지수부와 가수부로 나눠서 표시하므로, 소숫점의 위치가 변할 수 있다는 의미로 부동소수라고 한다. 문자형 char, unsigned char 열거형 enum void형 void 확장형 배열형 함수형 포인터형 구조체형 데이터의 크기 32비트 머신에서 int는 4바이트의 크기를 가진다. 데이터의 크기는 사용중인 컴퓨터와 컴파일러에 따라 다르다. 1 #include <stdio.h> 2 3 /* 4 자료형의 크기 5 */ 6 7 int main () 8 { 9 10 printf ( "int : %d \n " , sizeof ( int )); 11 printf ( "short : %d \n " , sizeof ( short )); 12 printf ( "long : %d \n " , sizeof ( long )); 13 printf ( "long long : %d \n " , sizeof ( long long )); 14 printf ( "float : %d \n " , sizeof ( float )); 15 printf ( "double : %d \n " , sizeof ( double )); 16 printf ( "char : %d \n " , sizeof ( char )); 17 18 syste