Cannot cast array to pointer

前端 未结 8 980
陌清茗
陌清茗 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-15 00:13

    The existing answers, though correct, don't make it very clear that there is a fundamental reason (apart from the language rules) why you cannot cast char [10][10] to char **. Even if you force the cast by saying something like

    char arr[2][2];
    char ** ptr = (char **)arr;
    

    it won't actually work.

    The reason is that in C and C++ a two-dimensional array is laid out in memory as an array of arrays. That is, in C a two-dimensional array is laid out in memory as a single allocation,

    arr -> arr[0][0]
           arr[0][1]
           arr[1][0]
           arr[1][1]
    

    You'll notice that arr doesn't point to a char * but to arr[0][0] which is a char; therefore, while arr can be cast to a char *, it cannot be cast to a char **.

    The correct forced cast would be

    char arr[2][2];
    char * ptr = (char *)arr;
    

    If you don't want to force the cast (always a good idea if possible!) you would say

    char arr[2][2];
    char * ptr = arr[0];
    

    or, to make the outcome clearer,

    char arr[2][2];
    char * ptr = &arr[0][0];
    

    And you now have (in effect) a pointer to an array of 4 characters. [Proviso: I can't find anything in the C standard that prohibits an implementation from adding padding between two rows of an array, but I don't believe that any real-world implementations do so, and common coding practice depends on the assumption that there will be no such padding.]

    If you really needed to convert arr to a char ** you would have to explicitly create an array of pointers:

    char arr[2][2]
    char * arr_ptrs[2];
    char ** ptr;
    arr_ptrs[0] = arr[0];
    arr_ptrs[1] = arr[1];
    ptr = arr_ptrs;
    

    The C language could in principle do this for you automatically if you tried to cast a two-dimensional array to a pointer-to-pointer but that would violate the programmer's expectation that a cast does not have side-effects such as allocating memory.

    In Java, by way of comparison, a two-dimensional array is always an array of pointers to arrays, so that the array

    char arr[][] = { {'a', 'b'}, {'c', 'd'} };
    

    is laid out in memory as three separate allocations, in arbitrary order and not necessarily adjacent,

    arr -> arr[0]
           arr[1]
    
    arr[0] -> arr[0][0]
              arr[0][1]
    
    arr[1] -> arr[1][0]
              arr[1][1]
    

    You will immediately notice that this requires more memory than the equivalent C array, and is slower to evaluate at runtime. On the other hand, it does allow the rows of an array to have different lengths.

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

    Arrays are not pointers (I notice a lot of books tend to make you think this, though). They are something completely different. A pointer is a memory address while an array is a contiguous set of some data.

    In some cases, an array can decay to a pointer to its first element. You can then use pointer arithmetic to iterate through the contiguous memory. An example of this case would be when passing an array to a function as a parameter.

    What you probably want to do here is something like:

    char arr[10];
    char * i = &arr[0];
    

    Obviously you'll need to use 2D arrays and char** in your case. I'll leave that to you to figure out :)

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