#void pointer casting help

9 messages · Page 1 of 1 (latest)

weak flare
#

Say i have a function Foo that returns a void * which in my case will actually return a string, if i want the first character of that string is it correct to do ((char *)Foo())[0] ?

worthy cloudBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

weak flare
#

void pointer casting help

tribal chasm
#

Looks good to me but you can just try it:

#

;compile

#include <stdio.h>

void *foo() {
    return (void *) "abc";
}

int main(void) {
    char *s = "abc";
    printf("%c%c", ((char *) foo())[0],
    ((char *) foo())[1]);
}
glacial raftBOT
#
Program Output
ab
frail nymph
#

If your function returns NULL it will crash though.

weak flare
#

!solved