0

The following openGL code APPARENTLY passes display function as an argument to glutDisplayFunction. In reality only a function pointer can be passed as argument to glutDisplayFunction. I wonder how this program is running successfully?

#include <GL/glut.h>
#include<GL/gl.h>// Header File For The GLUT Library

void init(void)
{
    glClearColor (0.0,0.0,0.4,0.0);
    glShadeModel(GL_FLAT);
}

void reshape (int w, int h)
{
    glViewport(0,0, (GLsizei) w, (GLsizei)h);   // indicates the shape of the available screen area into which the scene is mapped
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-10,10,-10,10,-10,10);


}

void display (void)
{
    int i,j;

    while(1){
    for (i=-10; i <=10; i++){
    glClearColor (1.0,0.0,0.0,0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.0, 0.0, 1.0);
    glBegin(GL_TRIANGLES);
        glVertex3f(i,2,5);
        glVertex3f(6,-i,-5);
        glVertex3f(1,9,-1);
    glEnd();
    glFlush();}
    }
}

int main (int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(500,500);
    glutCreateWindow (argv[0]);
    init ();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop ();
    return 0;
}
1
  • 1
    It actually passes the address of those two functions. Try the following: printf("funcAddress: %p\n", somFuncName); produces the exact same result as printf("funcAddress: %p\n", &someFuncName);
    – enhzflep
    Apr 11, 2014 at 2:45

2 Answers 2

3

We need not use & operator to create a reference to a function.We can pass 'display' to 'glutDisplayFunc' directly. Something Like array name referring to address of first element?

This -glutDisplayFunc(&display) - also should be fine

3

Passing the name of a function without parentheses as an argument implicitly passes a pointer to the function. It is not possible to pass a function by value in C.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.