Cannot cast array to pointer

前端 未结 8 965
陌清茗
陌清茗 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: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 :)

提交回复
热议问题