문제

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?

올바른 솔루션이 없습니다

다른 팁

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.

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.

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top