Thread: void* error

  1. #1
    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,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    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, 06: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