What argument for a function? for static array variable

This is a discussion on What argument for a function? for static array variable within the C++ Programming forums, part of the General Programming Boards category; Hi, i have little problem, i started to play with opengl/c++. i got a code like: Code: glBegin (GL_TRIANGLES); for(int ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    4

    What argument for a function? for static array variable

    Hi, i have little problem, i started to play with opengl/c++.
    i got a code like:
    Code:
    glBegin (GL_TRIANGLES);
      for(int i=0;i<sizeof(bodykit_face_indicies)/sizeof(bodykit_face_indicies[0]);i++){
    	   for(int j=0;j<3;j++){
    		  int vi=bodykit_face_indicies[i][j];
    		  int ni=bodykit_face_indicies[i][j+3];
    		  glNormal3f (normals[ni][0],normals[ni][1],normals[ni][2]);
    		  glVertex3f (vertices[vi][0],vertices[vi][1],vertices[vi][2]);
    		}
       }
    glEnd ();
    where bodykit_face_indicies is defined as :
    Code:
    static GLint bodykit_face_indicies[1921][6] = {.....}
    that works perfectly, but i split big object into small gropus and now i want draw each of them, using fuction like this:
    Code:
    void draw(GLint array){
       glBegin (GL_TRIANGLES);
       for(int i=0;i<sizeof(array)/sizeof(array[0]);i++){
    	   for(int j=0;j<3;j++){
    		  int vi=array[i][j];
    		  int ni=array[i][j+3];
    		  glNormal3f (normals[ni][0],normals[ni][1],normals[ni][2]);
    		  glVertex3f (vertices[vi][0],vertices[vi][1],vertices[vi][2]);
    		}
    	}
    	glEnd ();
    };
    and i want use it like :
    Code:
     
    draw(bodykit_face_indicies);
    how i have to declare the variable array? couse for GLint ot GLint* return compiler errors:
    for GLint:
    invalid types `GLint[int]' for array subscript
    for GLint*:
    invalid types `int[int]' for array subscript
    for GLint**:
    cannot convert `GLint (*)[6]' to `GLint**' for argument `1' to `void draw(GLint**)'

    thanks for any help/tips.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Here's how you pass a 2 dimensional array to a function.
    Code:
    #include <iostream>
    
    void pass_2d_array(int (*array)[3]);
    
    int main(void) {
        int array[5][3];
        pass_2d_array(array);
        return 0;
    }
    
    void pass_2d_array(int (*array)[3]) {
    
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    4
    thanks for reply but,
    i wrote function:
    Code:
    void draw(GLint (*array)[3]){
       glBegin (GL_TRIANGLES);
       for(int i=0;i<sizeof(array)/sizeof(array[0]);i++){
    	   for(int j=0;j<3;j++){
    		  int vi=array[i][j];
    		  int ni=array[i][j+3];
    		  glNormal3f (normals[ni][0],normals[ni][1],normals[ni][2]);
    		  glVertex3f (vertices[vi][0],vertices[vi][1],vertices[vi][2]);
    		}
    	}
    	glEnd ();
    };
    and when i am trying use it:
    draw(bodykit_face_indicies);

    compiler returns error:
    cannot convert `GLint (*)[6]' to `GLint (*)[3]' for argument `1' to `void draw(GLint (*)[3])'

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    So change the [3] to a [6] in the function declaration.

    > sizeof(array)/sizeof(array[0])
    This only works on arrays in scope, not arrays passed as parameters.
    Pass the size as a separate parameter to the function if you need to.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    4
    Thanks , that solved the problem. At first, i changed it to [6], compiled and run but object was invisible, the problem was in ,as u mentioned, in scope of array.

    in the other hand.. this is kind of wierd, this array is 2 dimensional so why it must be declared as (*array)[6]?

    ahh i am slow i guess. it is works like this?:
    fo 3 dim array let says: a[10][2][7] it should be write as (*array)[2][7]?

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    3 dim array let says: a[10][2][7] it should be write as (*array)[2][7]?
    Yes. Or:
    Code:
    array[][2][7]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    4
    well but there is another question in that case... what if i got 2 arrays:
    Code:
    a[10][5]={};
    b[12][3]={};
    and i want to use them in function.. (*array)[5] will wont work for b...
    and if i define as (*array)[] complier returns error. so first parameter of array can be any but second have to be the constant?

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Yes, the second one has to be a constant. Well, sort of:
    Code:
    void a_function(int size, int array[][size]);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 06:18 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21