아하 로고
검색 이미지
생활꿀팁 이미지
생활꿀팁생활
생활꿀팁 이미지
생활꿀팁생활
얄쌍한바다매186
얄쌍한바다매18620.06.14

C/C++ 배열은 어떤 타입인가요?

#include <iostream> #include <typeinfo> using std::cout; using std::endl; template<typename T> decltype(auto) type(T t) { return typeid(t).name(); } #define printType(v) cout << typeid(v).name() << endl; int main() { int arr[10][10]; printType(arr) // A10_A10_i (int[10][10]) auto copy = arr; printType(copy) // PA10_i (int(*)[10]) auto reference = &arr; printType(reference) // PA10_A10_i (int(*)[10][10]) auto dereferenced = *reference; printType(dereferenced) // PA10_i (int(*)[10]) auto byPointer = type<int (*)[10]>(arr); cout << byPointer << endl; // PA10_i (int(*)[10]) auto byArray = type<int[999][10]>(arr); cout << byArray << endl; // PA10_i (int(*)[10]) auto byAuto = type(arr); cout << byAuto << endl; // PA10_i (int(*)[10]) auto referenceByAuto = type(&arr); cout << referenceByAuto << endl; // PA10_A10_i (int(*)[10][10]) return 0; }

위 코드에서 int arr[10][10];으로 int[10][10] 타입의 변수 arr을 선언했습니다.

그러나 &arr은 int(*)[10][10] 타입, *&arr이나 arr로부터 만든 변수도 int(*)[10] 타입으로

어떤 방법으로도 arr로부터 int[10][10] 타입의 변수를 선언&초기화하지 못했습니다.

(decltype(auto) decltypeAuto = arr;는 오류 (array initializer must be an initializer list))

배열은 정확히 어떤 타입인가요? 포인터와 무엇이 다른가요?
배열 타입의 변수는 어떻게 다른 변수로부터 초기화할 수 있나요?

&arr은 int(*)[10][10]인데 *&arr은 왜 int[10][10]이 아닌 int(*)[10]인가요?
배열, 배열 포인터, 포인터 배열을 참조/역참조할 때는 어떤 규칙을 따르나요?

55글자 더 채워주세요.
답변의 개수1개의 답변이 있어요!
  • Q. 배열은 정확히 어떤 타입인가요? 포인터와 무엇이 다른가요?
    A. 배열은 non-modifiable lvalue이며, 대입 연산을 사용 할 수 없습니다.

    A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a const-qualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a const-qualified type.An assignment operator shall have a modifiable lvalue as its left operand.

    Q. 배열 타입의 변수는 어떻게 다른 변수로부터 초기화할 수 있나요?

    A. memcpy 쓰시면 될거 같습니다.

    Q. &arr은 int(*)[10][10]인데 *&arr은 왜 int[10][10]이 아닌 int(*)[10]인가요?

    A. C언어 표준에 있는 내용을 참고하시면 될거 같습니다.

    Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type "array of type" is converted to an expression with type "pointer to type" that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined

    Q. 배열, 배열 포인터, 포인터 배열을 참조/역참조할 때는 어떤 규칙을 따르나요?

    A. 위의 답변 참고하면 될거 같습니다.

    추가로 아래 두 링크를 보시면 더 도움이 될 것이라고 생각합니다.

    https://stackoverflow.com/questions/17687429/why-array-type-object-is-not-modifiable/17691191#17691191

    http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf