2D int array


Hello!

I have a function, which needs input as func(int tetras[][4]);

Now i can not seem to figure out how to properly declare this input.
I have

1
2
3
4
5
  int nTriangles;

 //some code to get the nTriangles

int tetras[nTriangles][4];


But it gives an error:


1>Compiling...
1>MyComputeThreshold1.cpp
1>.\MyComputeThreshold1.cpp(159) : error C2057: expected constant expression
1>.\MyComputeThreshold1.cpp(159) : error C2466: cannot allocate an array of constant size 0
1>.\MyComputeThreshold1.cpp(159) : error C2133: 'tetras' : unknown size



Any help or advices appreciated!

Also:
1
2
3
int **tetras = new int* [nTriangles];
for (int i = 0; i < nTriangles; i++)
    tetras[i] = new int[4];



Gives an error:

1>Compiling...
1>MyComputeThreshold1.cpp
1>.\MyComputeThreshold1.cpp(181) : error C2664: 'HxTetraGrid::HxTetraGrid(McVec3f *,int,int [][4],int)' : cannot convert parameter 3 from 'int **' to 'int [][4]'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

The value nTriangles is only known at runtime, but for you to declare tetras like that: int tetras[nTriangles][4]; it would have to be a compile time constant.
closed account (D80DSL3A)
Try: int (*tetras)[4] = new int[nTriangles][4];
nTriangles can be variable here.

If func() relies on sizeof(tetras) to find the # of rows this won't work though.

EDIT: for cleanup: delete [] tetras;
Last edited on
Thanks!

That one did the trick!
Topic archived. No new replies allowed.