Is it possible to add pointer to void to unsigned short?

naseeam
naseeam used Ask the Experts™
on
Is it possilbe to add pointer to void to unsigned short?


extern void * memcpy(void *, const void *, size_t);

unsigned char
somefunc_validate(void         *data,
                      unsigned_16   validate_entries,
                      void         *validate_cfg
                     )  {
..
..

unsigned short    min_value;

..

memcpy(&max_value, validate_cfg + sizeof(min_value), sizeof(max_value));

What is the result of:    validate_cfg + sizeof(min_value)

Above is working code but after compiling with upgraded compiler, following error occurs:
illegal type(s):  ptr-to-void '+' unit
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Your not allowed to use void pointers with pointer arithmetic to my knowledge.  I'm surprised you didn't get an error with your old compiler.
-w00te

Commented:
Oh, I think you might be able to in C though.  Maybe you switched to a C++ compiler which woudln't allow it?
gcc would like it and g++ wouldnt I think.
-w00te

Author

Commented:
I didn't switch to c++ compiler.  I switched from WindRiver diab 4.2b  C langauage compiler to WindRiver diab 5.8.0.0 C languge compiler.

How can this error be fixed without changing functionality?  Maybe a typecast is needed.
I don't want to change the pointer arithemetic functionality but I need help in resolving this error.
Top Expert 2012
Commented:
>>How can this error be fixed without changing functionality?  Maybe a typecast
>>is needed.

Exactly like that. A cast to 'char*' should do it, e.g.
memcpy(&max_value, (char*)validate_cfg + sizeof(min_value), sizeof(max_value));

Open in new window

Commented:
This works, change it to a char pointer before changing the arithmetic, they have the same internal representation aparently.
http://www.velocityreviews.com/forums/t316242-void-pointer-arithmetic.html
-w00te

int main() {
    void* someptr;
    int x = 14;
    someptr = &x;
  
    unsigned short someshort = 12;

    printf("%d", someptr);

    //conversion here, its the only important part.
    char* temp = (char*)someptr;
    temp += someshort;
    someptr = (void*)temp;

    printf("%d", someptr);

}

Open in new window

Commented:
Whoops, he beat me to it haha.  Sorry bout that JKR.

Author

Commented:
Thanks for all the effort and comments.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial