type casting void *

This is a discussion on type casting void * within the C Programming forums, part of the General Programming Boards category; Does following code compile in any compiler ? ... int * p,*q; void *vp; int a=5; p=&a; vp=p; // I ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    New York
    Posts
    24

    type casting void *

    Does following code compile in any compiler ?


    ...
    int * p,*q;
    void *vp;

    int a=5;
    p=&a;
    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,
    A

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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)
    Code:
    char *p = malloc(3);
    but not in C++.

    In both languages, you can assign any pointer to a void pointer without a cast.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    It may be that the Visual C++ 6 compiler is pretty outdated and not very standards compliant. You should probably upgrade your compiler.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Location
    New York
    Posts
    24
    Quote Originally Posted by dwks View Post
    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.
    I got the point from yours and Elysia's reply.

    Thanks to u both,
    A

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    It may be that the Visual C++ 6 compiler is pretty outdated and not very standards compliant. You should probably upgrade your compiler.
    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].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 04:15 AM
  4. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21