views:

16

answers:

1

Ok, this compiles fine in GCC under Linux.

char * _v3_get_msg_string(void *offset, uint16_t *len) {/*{{{*/
    char *s;
    memcpy(len, offset, 2);
    *len = ntohs(*len);
    s = malloc(*len+1);
    memset(s, 0, *len+1);
    memcpy(s, offset+2, *len);
    s[*len] = '\0';
    *len+=2;
    return s;
}/*}}}*/

However, I'm having a problem porting it to Windows, due to the line...

memcpy(s, offset+2, *len);

Being a void pointer, VC++ doesn't want to offset the pointer. The usual caveat that CPP doesn't allow pointer offsets SHOULD be moot, as the whole project is being built under extern "C".

Now, this is only 1 function in many, and finding the answer to this will allow them all to be fixed. I would really prefer not having to rewrite the library project from the ground up, and I don't want to build under MinGW. There has to be a way to do this that I'm missing, and not finding in Google.

+2  A: 

Well, you cannot do pointer arithmetics with void*, it is ridiculous that this compiles under GCC. try memcpy(s, ((char*)offset)+2,*len);

Yossarian
GCC provides many extensions to standard C, including a lot of very useful ones. See http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_4.html for a list. In particular, it allows pointer arithmetic on void-pointers: see http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_4.html#SEC78 for details. If you compile with the '-pedantic' option, it will warn you of non-standard constructs in your program.
TonyK
This is working, and TonyK is spot on with his comment. Its allowed in GCC, and has its uses, but its not very portable.
FWishbringer
Well, there are some quite useful/interesting extensions, but this one I'd ban. :-). @TonyK thanks for the link.
Yossarian