Cannot cast array to pointer

前端 未结 8 966
陌清茗
陌清茗 2020-12-14 23:41

I have the following source:

#include 
using namespace std;

void main(int j)
{
    char arr[10][10];
    char** ptr;
    ptr = arr;
}


        
相关标签:
8条回答
  • 2020-12-14 23:53

    The types char[10][10] and char** and char (*)[10] are all different types. However, the first one cannot convert into the second one, it can convert into the third one.

    So try this:

    char arr[10][10];
    char (*ptr)[10];
    ptr = arr; //ok
    

    It will work, because as I said object of type char[10][10] can convert into an object of type char (*)[10]. They're compatible types.

    0 讨论(0)
  • 2020-12-14 23:59

    The error exactly tells you whats wrong a double dimensional array can be assigned to an pointer to array not an double pointer. So what you need is:

    char (*ptr)[10] = arr; 
    

    What am I doing wrong?

    First things first
    Arrays are not pointers!! but they act sometimes like pointers.

    The rule is:

    An expression with array type (which could be an array name) converts to a pointer anytime an array type is not legal, but a pointer type is.

    So if you have a single dimensional array:

    char arr[10];
    

    Then arr decays to address of the zeroth element it has the type char *. Hence:

    char *ptr = arr;
    

    But if you have an 2 dimensional array which is essentially an array of arrays.

     char arr[10][10];
    

    Then arr decays to the pointer to an array of 10 characters.

    So, In order to assign arr to something, you will need that something to match the type, which is pointer to an array of characters.
    Hence:

    char (*ptr)[10] = arr; 
    
    0 讨论(0)
  • 2020-12-15 00:00

    When you cast ar[10][10] to pointer you will get a array of pointer as said above *ar[10] and not **ar.

    0 讨论(0)
  • 2020-12-15 00:09

    I thought arrays in c++ were just pointers.

    No, an array is a set of objects laid out contiguously in memory. In some circumstances, they are convertible to a pointer to the first element.

    So a char[][] could also be char**

    No. It is convertible to a pointer to the first one-dimensional array (which is the type char (*)[10] mentioned in the error message); but that array is not a pointer, so it is not convertible to a pointer-to-pointer.

    0 讨论(0)
  • 2020-12-15 00:12

    Arrays aren't pointers.

    An array decays to a pointer in most circumstances, but this isn't recursive. So a T[] decays to a T *, but a T[][] doesn't decay to a T**.

    I suggest reading the whole of the C FAQ chapter on arrays and pointers; in particular, the section on 2D arrays and pointers-to-pointers.

    0 讨论(0)
  • 2020-12-15 00:12

    Arrays are NOT just pointers -- arrays are arrays. Arrays are not first-class types, however, so you can't use them in many places. The thing that causes your confusion is that array names CAN be implicitly converted into pointers to the array's first element, which means that you can use an array in many places where you need a pointer and it 'just works'. This, however, is not one of those places.

    0 讨论(0)
提交回复
热议问题