Link to home
Start Free TrialLog in
Avatar of naseeam
naseeamFlag for United States of America

asked on

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

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
SOLUTION
Avatar of w00te
w00te
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of naseeam

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Whoops, he beat me to it haha.  Sorry bout that JKR.
Avatar of naseeam

ASKER

Thanks for all the effort and comments.