INFO: Casting a Pointer to Type Equivalent to Multidimensional (43703)
The information in this article applies to:
- Microsoft C for MS-DOS
- Microsoft Visual C++ 1.0
- Microsoft Visual C++ 1.5
- Microsoft Visual C++ 1.51
- Microsoft Visual C++ 1.52
- Microsoft Visual C++ 2.0
- Microsoft Visual C++ 2.1
- Microsoft Visual C++ 4.0
- Microsoft Visual C++, 32-bit Enterprise Edition 5.0
- Microsoft Visual C++, 32-bit Enterprise Edition 6.0
- Microsoft Visual C++, 32-bit Professional Edition 5.0
- Microsoft Visual C++, 32-bit Professional Edition 6.0
- Microsoft Visual C++, 32-bit Learning Edition 6.0
This article was previously published under Q43703 SUMMARY
In some situations you may need to cast a pointer or an address to a type
that is equivalent to a multidimensional array. The following example
demonstrates such a situation:
typedef char Arr2Dim [][20] ;
void myfunc (Arr2Dim) ;
char * ptr ;
...
void main (void)
{
...
myfunc ( (Arr2Dim) ptr ) ; /* illegal */
...
}
Casting the variable "ptr" to the array type "Arr2Dim" is not correct. The
Microsoft C Compiler displays the following error message:
error C2067: cast to array type is illegal
The correct procedure is to cast the pointer "ptr" to a pointer type
equivalent to the array type Arr2Dim. This pointer type can be defined as
follows:
typedef char (*Ptr2Dim) [20] ;
Casting "ptr" to the type of "Ptr2Dim", as follows, is correct and produces
no warning messages when compiled at warning level 3:
myfunc ( (Ptr2Dim) ptr ) ;
The address (or pointer) passed to the function is used correctly.
Modification Type: | Minor | Last Reviewed: | 7/5/2005 |
---|
Keywords: | kbinfo kbLangC KB43703 |
---|
|