Like Tree3Likes
  • 3 Post By hk_mp5kpdw

Void pointer

This is a discussion on Void pointer within the C Programming forums, part of the General Programming Boards category; What is a void pointer?Consider the following code snippet Code: #include<stdio.h> void main() { float i=10,*j; void *k; k=&i; j=k; ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    25

    Void pointer

    What is a void pointer?Consider the following code snippet
    Code:
    #include<stdio.h>
            void main()
              {
                  float i=10,*j;
                  void *k;
                  k=&i;
                j=k;
                  printf("%f",*j);
            }
    What happens when the line void *k is encountered?
    How is the assignment done?Does that mean k holds the address of i?Also,why doesn't the following code work?
    Code:
    #include<stdio.h>
            void main()
              {
                  float i=10,*j;
                  void *k;
                  k=&i;
                  printf("%d",*k);
            }
    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    Quote Originally Posted by Sylar Persie
    What happens when the line void *k is encountered?
    How is the assignment done?Does that mean k holds the address of i?
    Yes, k points to i.

    Quote Originally Posted by Sylar Persie
    Also,why doesn't the following code work?
    The type information is lost, hence you cannot dereference a pointer to void.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    25
    Quote Originally Posted by laserlight View Post


    The type information is lost
    I'm sorry,what does that mean?Could you elaborate?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    A void pointer is just taken to mean a raw generic address, not an address pointing to a specific data type. When you dereference a pointer you need to know what type it is supposed to be pointing at. Given a generic pointer to some address, the compiler rightly complains when you attempt to dereference it since it cannot determine what it should be pointing to. You - the programmer - can logically follow the source and see that it is pointing to an int or a float or whatever but the compiler does not have that luxury, its simply an address without any associated type information.

    Consider this, if you dereference an integer pointer, the compiler knows to take sizeof(int) bytes at the address the pointer points to, and treat those sizeof(int) bytes as an integer. If you have a double pointer that for whatever reason points to the same address as the above integer pointer, when you dereference the double pointer, it is going to take sizeof(double) bytes and interpret as a double value. If you have a generic void pointer to that same address, how does the compiler know how many bytes to grab when you try to dereference the pointer if there is no type associated with the pointer.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    25
    Thanks hk_mp5kpdw....that made things clear

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Casting to pointer to pointer to void
    By Sharke in forum C Programming
    Replies: 13
    Last Post: 05-12-2009, 08:40 PM
  2. Replies: 4
    Last Post: 08-27-2007, 11:51 PM
  3. Dereference pointer to void pointer to member
    By phil in forum C Programming
    Replies: 5
    Last Post: 04-20-2005, 11:54 AM
  4. void pointer
    By frenchfry164 in forum C++ Programming
    Replies: 2
    Last Post: 11-02-2003, 01:31 PM
  5. Need help on pointer to VOID
    By dv007 in forum C Programming
    Replies: 19
    Last Post: 07-09-2002, 06:15 PM

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