Returning a pointer from a function is a common operation in C/C++. In this paper, we do some research on returning a pointer to a two-dimensional array, and conclude that a pointer to a two-dimensional array returns a pointer of type double **.

​​

​​

Regular C/C++ functions return a pointer to a two-dimensional array

Returns a pointer to a two-dimensional array

Let’s start with a simple case of returning a two-dimensional pointer via a return value:

​​

​​

In the code above, the data memory is allocated from the heap, so the allocated storage is still available after the function ends. Call method:

​​

​​

(2) Return a pointer to a two-dimensional array

In some cases, we want to pass in a pointer that does the memory allocation inside the function. This requirement is a little more complicated and the code is:

​​

​​

To change the pointer to a 2-d array, we need to pass a 2-D array pointer by reference, so the argument is of type double***. Code call:

​​

​​

In contrast to the return value mode, the parameter mode is a little more difficult to understand, but also easy to understand.

Returns a double** pointer to a two-dimensional static array

This brings us to the focus of this article: returning a double** pointer to a two-dimensional static array.

At first glance, this requirement is easy to fulfill:

​​

​​

Unfortunately, the above code does not compile and causes the following error:

error: cannot initialize return object of type ‘double **’ with an lvalue of type ‘double [10][10]’

A refers to double [10][10], but the return value requires double**.

Compiler does not know how to cast, try cast?

​​

​​

OK, compilation is OK, but the problem turns to calling:

​​

​​

The problem is the conversion of A: A in A[N][N] is actually A one-dimensional array, each value holding A pointer.

Verify our idea with the following code:

​​

​​

Call the above code to re-run the program, perfect, everything works as expected!

We managed to get the program running by introducing Pointer1D, but we know that Pointer1D is an alias for double*, so we can actually write code as double* directly:

​​

​​

Running the program, it’s perfectly fine. So far we have successfully returned a double** pointer from a two-dimensional static array.

But one question remains: must A new variable be introduced and initialized, instead of returning A directly (or via A cast)?

The answer is no, because there is information loss in returning A directly (or through A cast).

Take the 3d square matrix as an example, double A[3][3] actually saves 12 data information: 9 save the value of the matrix, which is easy to understand; In addition, three additional double* data points to the start of each line of A. With these 12 data, A[I][j] will actually be converted into the following values:

​​

​​

That is, the pointer of the row is obtained according to I, and then the pointer of A[I][j] is obtained according to j offset, and finally A[I][j] is obtained.

A in A[N][N] is essentially A one-dimensional array that holds only the values of three double* Pointers. Cast to double** : double** a = (double**) a, a is exactly the same as a, but a[I] = a[I]!

This can be verified by the following code:

​​

​​

The output result is:

​​

​​

A is indeed equal to a, but a[0] is not equal to a[0]! Because a[0] is not assigned, the program will simply hang if a[0] is given. .

This is why the working code above requires an extra array to be introduced and initialized to work properly.

To generate a two-dimensional array, not only do we need double** data, but we also need to generate an array containing n double* s and initialize it:

​​

​​

Segment Fault [I] = Segment Fault [I]; Segment Fault [I] = Segment Fault [I];

conclusion

This paper introduces three methods of C/C++ functions to return Pointers to two-dimensional arrays, and deeply analyzes the problems and solutions of converting two-dimensional static arrays to double**. In fact, a static two-dimensional array returning double** is equivalent to the question: How do I convert a two-dimensional array to a double** pointer?

From the above analysis, we know that direct conversion is not feasible. We need to introduce an extra array and initialize it to get the conversion right.

Wechat official account: C language programming Learning base

Share (source code, project actual combat video, project notes, basic introduction tutorial)