Giorgos Fandomas
Giorgos Fandomas

Reputation: 171

C - Casting char array to an integer pointer?

I have the following source code and there is one line, which i cannot understand the casting is been made. Can anyone explain please? I know the casting to an integer pointer (int *) but this is different. I cannot understand what the final line does. Is it returning an integer pointer? or i am wrong?

const unsigned char sc[] =  {  0x01, 0x01, 0x01, 0x01   };
return ((int (*)(void))sc)();

Upvotes: 1

Views: 886

Answers (1)

user694733
user694733

Reputation: 16047

(int (*)(void))sc takes address of an array sc and converts it to a pointer to function which returns int.

() at the end then calls this function, and return value of this function (type int) is again returned.

Weird part is that it seems as if the intention was to call function at address 0x01010101, but it uses the address of array sc instead, which may be an error.

Upvotes: 4

Related Questions