type casting void *
Does following code compile in any compiler ?
int * p,*q;
vp=p; // I know this perfectly ok
q=vp; // here as I know it will not compile..and we need to type cast (int *).
The visual studio C++ 6 compiler said the same.But a leading author in C and C++ says
in his book : "No typecasting is required while assigning the value to and from vp because conversion are applied automatically when other pointers type are assigned to and from void*". So no error for the code.
Am I doing something wrong to understand the concept here?
Thanks and Regards,
In C, you can assign a void pointer to any other pointer type. In C++, you cannot.
This is why you can do this in C (as malloc() returns a void * in C89)
but not in C++.
char *p = malloc(3);
In both languages, you can assign any pointer to a void pointer without a cast.
It may be that the Visual C++ 6 compiler is pretty outdated and not very standards compliant. You should probably upgrade your compiler.
It's also possible that the code was saved as a .cpp file, so MSVC tried to compile it as C++. This would probably result in an error about assigning void pointers. If you tell MSVC that this is really a C program by using a .c extension for your filename, it might compile it as such.
You are right dwks..its compling with out error with .c extension.
Originally Posted by dwks
I got the point from yours and Elysia's reply.
Thanks to u both,
That would be correct for C++, but this is C, and as far as I'm aware, the C side of Visual Studio 6 is OK. [I'm pressuming it's Visual Studio 6, rather than cl version 6 that is meant here, as cl version 6 would indeed be VERY old - coming up for 15 years or so - I can't find a concrete date of release in 2 minutes of googling, but it was prior to version 7.0, which was prior to Visual C++ 1.0, which came out in 1992 - so my guess would be closer to 1988 or so].
Originally Posted by Elysia