0

I would like to pass a 2D array as a parameter. The issue is that the size of the 2d array always varies. So I can't write something like myfunction(int a[2][5]);

I have a header file containing a lot of 3D-coordinates. e.g.:

float someArray[][3] = {{0.0,1.0,-2.0},{-1.0,1.0,-2.0},{-1.0,1.0,0.0},{0.0,1.0,0.0},
            {-1.0,0.75,-2.0},{-1.0,1.0,-2.0},{-1.0,1.0,0.0},{-1.0,0.75,0.0},
            {0.0,0.75,-2.0},{0.0,1.0,-2.0},{0.0,1.0,0.0},{0.0,0.75,0.0}, 
            {0.0,0.75,-2.0},{-1.0,0.75,-2.0},{-1.0,0.75,0.0},{0.0,0.75,0.0},
            {0.0,0.75,-2.0},{-1.0,0.75,-2.0},{-1.0,1.0,-2.0},{0.0,1.0,-2.0},
            }; 


void drawVertex(const float **vertex, int numberVertex, int shape)
{
    int i;

    glPushMatrix();
    switch(shape)
    {
        case 0: glBegin(GL_LINES); break;
        case 1: glBegin(GL_POLYGON); break;
        default: break;
    }

    for(i=0;i<numberVertex;i++)
    {
        glVertex3fv(vertex[i]);
    }
    glEnd();
    glPopMatrix();

}

the function calls I tried:

these just crash (core dump)

drawVertex(someArray, 6,1);
drawVertex(*someArray, 6,1);

Should I modify the parameters of the function drawVertex() ? I've tried many things but nothing seems work.

privetDruzia
  • 357
  • 4
  • 16
  • 1
    `const GLfloat **` is not a 2D array and cannot represent one. Use normal array notation or an explicit pointer to array (due do automatic conversion of format parameters they are identical). – too honest for this site Apr 04 '16 at 12:24
  • And after carrying out @Olaf's suggestion, can't you make it global? –  Apr 04 '16 at 12:26
  • Compiler warnings are not just to show some fancy messages. Don't ignore them and you are expected to have them enabled and payed heed to them before asking. – too honest for this site Apr 04 '16 at 12:27
  • 1
    @abhishek_naik: You hopefully mean "**can** you make it **not** global?" Anyway, as that is an initialised `const` qualified array, making it global is well acceptable. It just should have internal linkage, i.e. `static`. – too honest for this site Apr 04 '16 at 12:29
  • 1
    Possible duplicate of [C -- passing a 2d array as a function argument?](http://stackoverflow.com/questions/6862813/c-passing-a-2d-array-as-a-function-argument) – too honest for this site Apr 04 '16 at 12:32
  • @Olaf, however this solution gives this warning: warning: passing argument 1 of ‘drawVertex’ from incompatible pointer type [enabled by default] drawVertex(myShape,6,0); ^ mijnTest.c:21:6: note: expected ‘const GLfloat (*)[3]’ but argument is of type ‘GLfloat (*)[3]’ void drawVertex(const GLfloat vertex[][3], int numberVertex, int shape) ^ – privetDruzia Apr 04 '16 at 12:36
  • @privetDruzia You might have to drop the const correctness. C is a bit dumb when it comes to mixing const with arrays decaying into array pointers. – Lundin Apr 04 '16 at 12:43

2 Answers2

0

If you need an array, you must use an array. Pointer-to-pointers have little to do with arrays. Change the function to:

void drawVertex(const GLfloat vertex[][3], int numberVertex, int shape)

or better yet:

void drawVertex(int numberVertex, const GLfloat vertex[numberVertex][3] int shape)
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • I really like the second option you proposed, nice one. However the compiler says "numberVertex undefined". The issue is this I can't use #define because it is a variable that will change every time I call that function – privetDruzia Apr 04 '16 at 12:29
  • @privetDruzia: If you get such an error, you have a typo. Anyway, as the outermost dimension is not used anyway, there is no need to specify it. It is good for documentation purposes, though. – too honest for this site Apr 04 '16 at 12:30
  • 2
    @privetDruzia You might have a 26 years old compiler. Consider upgrading to one which is not older than 17 years. – Lundin Apr 04 '16 at 12:33
  • @Olaf, Ok I ll accept that answer. Didn't see the link you posted above. shall I delete this question? (to avoid being downvoted for duplication continuously...) – privetDruzia Apr 04 '16 at 12:34
  • @Lundin, what makes you think my compiler is that old? – privetDruzia Apr 04 '16 at 12:34
  • 1
    @privetDruzia I posted no link, someone else did. If your post is a duplicate, it will get closed. I'm not so sure I agree that it is a duplicate of the linked question though. – Lundin Apr 04 '16 at 12:35
  • @privetDruzia Because it seems it follows the C standard from 1990 rather than the one from 1999 or later, where variable-length arrays were introduced. – Lundin Apr 04 '16 at 12:37
  • @privetDruzia: Please add the compiler+version+how you call it to your question text. Ubuntu LTS still uses gcc 4.8 which defaults to gnu90 (C90+extensions). – too honest for this site Apr 04 '16 at 12:57
0

It looks like the input here is a pointer to a pointer const float **vertex, which I don't really understand. Can you define as void drawVertex(const float vertex[][3], int numberVertex, int shape)? Also, it looks like you are trying to input an array of constant values; however, the input someArray[][3] is not defined as an array of constants.

emotality
  • 12,795
  • 4
  • 39
  • 60
Jonathon S.
  • 1,928
  • 1
  • 12
  • 18