ptr = 9;
This is not an error. But it is not recommended to directly assign values to pointers as we cannot be sure of dereferencing them as actual memory address using * operator.
int *ptr=(void*)9;
This also does the same job as above. Only difference is we explicitly typecast 9 as an address which would avoid compiler warning.
So, both would work but both are to be avoided in good programming. If we need to use one, use the second one.
NB: These kind of questions are way out of GATE scope. You can see previous GATE questions here (see the ones with GATEXXXX tags).