Pointer to array in 2d array

Hi folks.
I have certain function (in .cpp source) that takes a pointer to an array of 8 integer values.
(The function is responsible for creating a Minor - term used in 1553 interface code)

I have 4 minors, so I created 2d array:

int A[4][8];

I send to the function of Minor1 the parameter: A[0]. I got odd
results, hence my question:
Is A[0] defined as the pointer to the first A[8] array?

Update

Do I need to pass the parameter as: &A[0]?
Last edited on
https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function

The major reason for problems when passing regular arrays, 1D or 2D or more, the array devolves into pointers, the function has no clue what the array dimensions are.

There are several different suggested ways to pass that 2D array, the method I see most common is:

void passFunc(int A[][8], int row_size)

And you pass the array's first dimension 4 as the second parameter, the 2nd dimension of the array is explicitly coded in the function's parameter list.

I personally would do one of three things (not necessarily in the order of what works best):

1. create a temp 1D array to hold the 2D array, passing that array and the 2D dimensions into the function.

void passFunc(int A_temp[], int row_size, int col_size)

2. upgrade the function to be a template function so the 2D dimensions are automatically passed to the function without being explicit parameters which need to be passed when calling the function.

3. create a temp stdlib 2D container, a vector of vectors, to hold the array's elements and pass the vector into the function. The vector doesn't devolve to a pointer and you can query each contained vector for its size.

C++20 added std::span to the toolbox which allows passing a 1D array or other container.
https://en.cppreference.com/w/cpp/container/span

I freely admit I am not all that conversant with std::span, I have yet to use it in any code I write other than brief testing snippets.

C++23 added std::mdspan to the toolbox, I am even less knowledgeable about this as I am std::span.
https://en.cppreference.com/w/cpp/container/mdspan

At first glance IMO this looks horribly complex and not at all intuitive as dealing with actual true multi-dimensional containers.

https://stackoverflow.com/questions/75778573/what-is-an-mdspan-and-what-is-it-used-for
yes, a[0] is the first location which is an ARRAY of 8, the A[0][...] row or sub-array.
You may treat it like a pointer (lines 16/17)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main() 
{
  int a[4][8]{};
   int* ip = a[0]; //you can also collapse to a 1d pointer of 4*8 size
   for(int i = 0; i < 8*4; i++)
	   ip[i] = i+1;
   
   for(int i = 0; i < 8; i++)
	   cout << a[0][i] << ' ' ;

   cout << "\nits the same thing\n";

   for(int i = 0; i < 8; i++)
      cout << *(a[0]+i) << ' ';

   cout << "\nfor fun\n";
   for(int i = 0; i < 8; i++)
      cout << *(a[2]+i) << ' ';

	  
}


so adding to the list above you can pass it in 1-d and reshape it inside (or just access it with 1d to 2d math: [desired row*numcols + desired col] .. its #1 without the tmp copy.
Last edited on
Thanks!

Does &A[0] is similar to A[0]? (As the function argument)
Yes, A[0] is equivalent to &(A[0][0]).
Likewise, A[1] is equivalent to &(A[1][0]).

So... check for other errors.
I found the error. When I defined the array:

 
A[const1][const2]


const1 was a variable. I replaced it with constant.

Thanks.
Ah, yes, you ran into magic VLA stuff.

Fix that by cranking up your compiler’s error levels. For MSVC that’s /W3 at minimum.
Topic archived. No new replies allowed.