2

My code is as follows:

void main()
{
    int gdriver=DETECT, gmode;

    initgraph(&gdriver, &gmode, " ");

    getch();

    closegraph();
}

Though the value of graphics mode(gmode) has not been initialized, its address has been passed into the program...and it compiles with no error!

Is it not wrong to pass the address of a variable without initializing that variable?

genpfault
  • 51,148
  • 11
  • 85
  • 139
krupal
  • 854
  • 2
  • 11
  • 17
  • 1
    `void main` alone is wrong: http://stackoverflow.com/q/204476/1619294 – Mark Garcia Feb 19 '14 at 05:36
  • 1
    You're passing a _pointer_ to the variable to the function, and the pointer isn't uninitialized, just what it points to. – Joachim Isaksson Feb 19 '14 at 05:39
  • 1
    If `initgraph` writes `gmode` before anyone reads it, where's the error? Now ask: how would the compiler looking at `main` know whether this is true or not? – asveikau Feb 19 '14 at 05:39

3 Answers3

2

You pass the address of gmode, not a reference to it. You don't need to initialize gmode, because the function initgraph() might do so.

You might get a warning though, depending on the compiler and the compiler options, you use.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
0

First, although you have tagged C++ as well, in the above code the passing of the argument is NOT pass by reference but pass by value.

graphics mode(gmode) has not been initialized

Second, it should not give you an error because it is not a reference variable.

Sadique
  • 22,572
  • 7
  • 65
  • 91
0

now I got the answer...when we write gdriver=DETECT, it returns the value for both - for gdriver and for gmode.

This is because DETECT is an enumerated data type that maps the gdriver and gmode to according graphics driver and mode value. That is why we do not require to initialize gmode before passing its address.

krupal
  • 854
  • 2
  • 11
  • 17