thedude said:
If I used this return statement in a c function
return (int)* "hello world";
Why would you do that?
From what I know the calling function will only retrieve the character
'h', right ?
The caller will receive the address of the string literal
cast to an int pointer. What that should be good for is
beyond me since the correct type of the address is const
char pointer. This conversion to int pointer invokes un-
defined behaviour. The standard allows to convert an ob-
ject pointer (e.g. an int *) to a void (or, for historical
reasons, char) pointer and then the back-conversion of this
result, but not from every object pointer type to each other
object pointer type (on some architectures different types
of pointers may have e.g. different sizes).
If you dereference the int pointer you got back from the func-
tion you may get a bus error since an int may have more strict
alignment requirements than a char, so the string may start at
an address that's not suitable aligned for accesses via an int
pointer (even if the cast from const char * to int * didn't
mess up the address).
Is it right to assume that nevertheless, read only memory was
allocated for the entire string "hello world"
Yes, with and without the "nevertheless";-)
And if so can that
memory(Or the rest of the characters) be accessed somehow ?
Yes and no. If your machine actually doesn't do anything
bad when converting from const char * to int * then you may
be able to convert the returned pointer back to const char *;
so you can get at the string. But why rely on that? If your
function is supposed to return a generic pointer you should
use a void pointer.
Regards, Jens