void* error

This is a discussion on void* error within the C Programming forums, part of the General Programming Boards category; hi all! I have a c program and at some point I do: Code: void *a; allocate(&a);//function that allocates a ...

  1. #1
    quo
    quo is offline
    Registered User
    Join Date
    May 2011
    Posts
    116

    void* error

    hi all!

    I have a c program and at some point I do:

    Code:
    void *a;
    allocate(&a);//function that allocates a pointer--this works ok
    memcpy(&b,a+25*sizeof(c),sizeof(int));//c is a struct
    the error I get while compiling is :

    error: pointer of type 'void *' used in arithmetic

    why is that?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,302
    The error message is self explanatory. Pointer arithmetic (or array indexing) does not work with void pointers.

    Pointer arithmetic can only be done if the compiler knows the size of the data the pointer points at. The whole point of a void pointer is that it can point at data of any type, so the size is indeterminate.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    quo
    quo is offline
    Registered User
    Join Date
    May 2011
    Posts
    116
    yes you're right!thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: invalid conversion from 'const void*' to 'void*'
    By Wahidin Wahid in forum C++ Programming
    Replies: 10
    Last Post: 04-17-2012, 02:17 AM
  2. error invalid conversion from ‘const void*’ to ‘void*’
    By Wahidin Wahid in forum C Programming
    Replies: 3
    Last Post: 03-27-2012, 08:18 PM
  3. void* error
    By Skep_ in forum C++ Programming
    Replies: 1
    Last Post: 12-02-2010, 05:16 AM
  4. Replies: 12
    Last Post: 03-27-2009, 02:36 PM
  5. Invalid conversion from 'const void*' to 'void*' error
    By prawntoast in forum C Programming
    Replies: 3
    Last Post: 05-01-2005, 10:30 AM

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