Void Pointers

This is a discussion on Void Pointers within the C Programming forums, part of the General Programming Boards category; Can the pointer arithmetic be done on the void pointer? If not, what to do when void pointer is having ...

  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
    21,439
    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.
    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
    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,185
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    your fptr is not initialized
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 03:14 AM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-01-2003, 11:26 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

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