Cannot assign array to pointer

I have the following source:

#include <iostream> using namespace std; void main(int j) { char arr[10][10]; char** ptr; ptr = arr; } 

when I compile it using VS2010, I get this error:

 error : a value of type "char (*)[10]" cannot be assigned to an entity of type "char **" 

I thought arrays in C ++ are just pointers. So char[][] can also be char** . What am I doing wrong?

+6
source share
8 answers

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

So try the following:

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

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

+4
source

Arrays are not pointers.

An array decays to a pointer in most cases, but this is not recursive. So, a T[] splits into T * , but a T[][] does not decrease until a T** .

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

+12
source

The existing answers, although correct, do not give an absolutely clear idea that there is a fundamental reason (other than language rules) why you cannot use char [10][10] for char ** . Even if you force the actors to say something like

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

he will not 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 one selection,

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

You will notice that arr does not point to char * , but to arr[0][0] , which is char ; therefore, while arr can be added to char * , it cannot be attributed to char ** .

The correct forced tide will be

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

If you do not 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 result clearer,

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

And now you have (in fact) a pointer to an array of 4 characters. [Proviso: I cannot find anything in the C standard that prohibits implementations from adding an additive between two lines of an array, but I don’t think that a real-world implementation does this, and the usual coding practice depends on the assumption that there will not be such an addition. ]

If you really needed to convert arr to char ** , you would need 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; 

C could, in principle, do this automatically for you if you tried to apply a two-dimensional array to a pointer to a pointer, but this would violate the programmer’s expectation that the actor has no side effects, such as memory allocation.

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

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

laid out in memory as three separate selections, in random order and not necessarily contiguous,

 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 it is slower to evaluate at runtime. On the other hand, this allows the rows of the array to have different lengths.

+5
source

I thought arrays in C ++ are just pointers.

No, an array is a collection of objects located in memory. In some cases, they are converted to a pointer to the first element.

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

Not. It is converted to a pointer to the first one-dimensional array (which is of type char (*)[10] mentioned in the error message); but this array is not a pointer, so it does not convert to a pointer to a pointer.

+1
source

The error tells you exactly that the wrong two-dimensional array can be assigned to a pointer to an array, not a double pointer. So you need to:

 char (*ptr)[10] = arr; 

What am I doing wrong?

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

Rule:

An expression with an array type (which may be the name of an array) is converted to a pointer at any time when the array type is not legal, but the pointer type.

So, if you have one dimensional array:

 char arr[10]; 

Then arr decays to the address of the zero element, it is of type char * . Consequently:

 char *ptr = arr; 

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

  char arr[10][10]; 

Then arr splits into a pointer to an array of 10 characters.

So, To assign arr to something, you need something to match the type, which is a pointer to an array of characters.
Consequently:

 char (*ptr)[10] = arr; 
0
source

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

0
source

Arrays are not pointers (I notice that many books tend to make you think about it). They are completely different. A pointer is a memory address, and an array is a continuous set of some data.

In some cases, an array can decay to a pointer to its first element. Then you can use pointer arithmetic to iterate through continuous memory. An example of this case is the passing of an array of functions as a parameter.

What you probably want to do here is something like:

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

Obviously you need to use 2D arrays and char ** in your case. I will leave it to you to find out :)

0
source

When you point ar [10] [10] to a pointer, you will get an array of pointers as above * ar [10], not ** ar.

0
source

Source: https://habr.com/ru/post/903606/


All Articles