Void pointer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ranjitshs
    New Member
    • Mar 2009
    • 6

    Void pointer

    Can the pointer arithmetic be done ou the void pointer?

    If not, Then how to increment a void pointer having address of integer or any other data type

    please explain with an example
  • mac11
    Contributor
    • Apr 2007
    • 256

    #2
    Originally posted by ranjitshs
    Can the pointer arithmetic be done ou the void pointer?

    If not, Then how to increment a void pointer having address of integer or any other data type

    please explain with an example
    The compiler does not know how large a 'void' is, so incrementing void pointers isn't wise. If you know what the type of data is then just cast to a pointer of that type and then increment.

    Code:
    void *v_ptr = &somedata;
    
    int *i_ptr = (int*)v_ptr;
    printf("here's the next int: %d", ++i);
    Clearly, this isn't the best example but you get the idea.

    Comment

    Working...