Thread: Void Pointers

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    14

    Unhappy Void Pointers

    Can the pointer arithmetic be done on the void pointer? If not, what to do when void pointer is having the
    address of some integer or other data type and we want to increment it?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by csvraju
    Can the pointer arithmetic be done on the void pointer?
    No.

    Quote Originally Posted by csvraju
    If not, what to do when void pointer is having the
    address of some integer or other data type and we want to increment it?
    You need to cast the void pointer to a pointer to the appropriate data type, increment the result, then assign it back.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    14
    is the declaration correct for type casting a void pointer

    *(char *)vptr;

    (*(char *)vptr)++; will the void pointer increment

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    This bit of code
    Code:
    (*(char *)vptr)++;
    will increment
    Code:
    (*(char *)vptr))
    which, if you read it out, is vptr, casted to a pointer-to-char, and then dereferenced to get a char. So, no, you will not increment the pointer.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    14
    using void pointers we can increment the value of char and integers .......


    but i am not able to increment the value of a float variable
    Code:
    #include <stdio.h>
    
    int main()
    {
        int i=10;
        char ch='B';
        float f = 9.8;
        int *iptr;
        char *cptr;
        float *fptr;
        void *vptr;
        
        iptr = &i;
            
        cptr = &ch;
            
        vptr = cptr;
        (*(char *)vptr)++;
        printf("Incremented value of ch is %c\n",ch);
        
        vptr = iptr;
        (*(int *)vptr)++;
        printf("incremented value of i is %d\n",i);
        
        vptr = fptr;
        (*(float *)vptr)++;
        printf("incremented value of f is %f\n",f);
        getch();
        return 0;
    }

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    your fptr is not initialized
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    14
    ok i got it.......

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    14
    the problem is solved

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. i cannot see the mouse arrow
    By peachchentao in forum C Programming
    Replies: 6
    Last Post: 12-10-2006, 04:14 AM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM