_countof 매크로

정적으로 할당 된 배열의 요소 수를 계산 합니다.

_countof( 
   array
);

매개 변수

  • array
    이름 배열입니다.

반환 값

배열에 있는 요소의 개수입니다.

설명

확인 array 실제로 배열, 하지에 대 한 포인터입니다.C에서 _countof 경우 잘못 된 결과 얻을 수 array 에 대 한 포인터입니다.C + +, _countof 는 경우 컴파일할 수 없습니다 array 에 대 한 포인터입니다.

요구 사항

매크로

필수 헤더

_countof

<stdlib.h>

예제

// crt_countof.cpp
#define _UNICODE
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>

int main( void )
{
   _TCHAR arr[20], *p;
   printf( "sizeof(arr) = %d bytes\n", sizeof(arr) );
   printf( "_countof(arr) = %d elements\n", _countof(arr) );
   // In C++, the following line would generate a compile-time error:
   // printf( "%d\n", _countof(p) ); // error C2784 (because p is a pointer)

   _tcscpy_s( arr, _countof(arr), _T("a string") );
   // unlike sizeof, _countof works here for both narrow- and wide-character strings
}
  

참고 항목

참조

sizeof 연산자