stl >> VC6's bug on casting multi-array to pointer

by chen3feng » Sat, 04 Oct 2003 22:20:54 GMT

int a[2][3][4];
const int (*p)[3][4] = a;

error C2440: 'initializing' : cannot convert from 'int [2][3][4]' to 'const
int (*)[3][4]'
Types pointed to are unrelated; conversion requires reinterpret_cast
, C-style cast or function-style cast




stl >> VC6's bug on casting multi-array to pointer

by Doug Harrison [MVP] » Sun, 05 Oct 2003 02:52:05 GMT






This is fixed in VC.NET.

--
Doug Harrison
Microsoft MVP - Visual C++



Similar Threads

1. Cast a pointer to array to base class pointer to array

Hi,

A have a pointer to an array of a derived class, which I want to cast to its 
base class. That is, im trying to do something like this:
void f( derivedClass* derivedData[] )
{
    baseClass* data[] = dynamic_cast<baseClass*[]>(derivedData);
}

This can't compile, so I'm wondering how to make it work?

Best Regards
Hansen 



2. casting pointers/arrays to multidimensional arrays

3. Array casting bug??

If variance/covariance is not allowed in array of value types, why is the 
expression on the "Main" method run successfully (see snippet below)? I am 
missing something?

Thank you.

---------------------
class Program
{
    enum Foo
    {
        a, b, c
    }

    enum Bar
    {
        x, y, z
    }

    static void Main(string[] args)
    {
        Bar[] barArray = (Bar[])System.Enum.GetValues(typeof(Foo));
    }
}
--------------------- 

4. Multi-dimentional array pointer

5. Pointer to Multi-Dim Array

I can't believe I can't figure this out!




#define MAX_COL    200
#define MAX_ROW   300

int MyArray[MAX_COL][MAX_ROW];

MyArrayFunction(VarArray[][MAX_ROW]);


Main()
{

// Fill Array....
....
// Call array Function
MyArrayFunction(MyArray);
...
}

MyArrayFunction(VarArray[][MAX_ROW])
{
// Do things to array
...
}



The above sample works fine. The problem is in "Main", I need to make a
pointer, to the multi-dim array. If I write:

int ArrayPointer [][MAX_ROW];

The compiler returns an error:  "can not convert  'int [][300]'  to
'int[][300]'.

How do you save a pointer to a multi-dim array?

Regards;

Bruce Kingsley



6. 2 questions: speed of memset() and pointer to multi-arrays

7. pointer arithmetic and multi-dimensional arrays

Dear experts,

according to my interpretation of the Standard, loop 1 in the
following program is legal, while loop 2 is not (see explanation
below). This looks a bit counterintuitive, though; do you know
the truth?

Thanks a lot in advance,
Bernd Gaertner.

#include<iostream>

int main()
{
   int a[2][3] = { {0, 1, 2}, {3, 4, 5} };
   int* first = a[0]; // pointer to a[0][0]

   // loop 1
   for (int* p = first; p < first+6; ++p)
     std::cout << *p; // 012345

   // loop 2
   for (int* p = first; p < first+6; p+=2)
     std::cout << *p; // 024

   return 0;
}

Explanation: [decl.array] 7-9 implies that the memory layout of
"int a[3][2]" is as for "int a[6]" - the six ints are consecutive
in memory. This means that during any iteration of loop 1, both p
and ++p are pointers to elements (or past-the-end pointers) of the
*same* three-element array, namely either a[0] or a[1]. In this
case, [expr.add] 5 guarantees well-defined behavior. In loop 2,
if p == first+2 (pointer to last element of a[0]), p+=2 points to
the second element of a[1], so p and p+=2 do not refer to the same
array. In this case, [expr.add] 5 stipulates undefined behavior.

8. multi-dimensional array/pointer help