Reputation: 336
I know there are questions asking if you can call a function without parentheses and the answer to that is no but in my code that works and I'd like to know why.
typedef struct{
//some variables
} REG;
long foo(){
//some code
return 23; //i.e 23, it could be any positive integer
}
REG * foo1(REG **ptr){
//some code
*ptr = calloc( (int) foo , sizeof(REG)); //foo without ()
//more code
fread(*ptr,sizeof(REG), foo(), fp);
return *ptr;
}
I'm compiling in xcode, it gives no error/warning.
Upvotes: 1
Views: 777
Reputation: 540
Maybe I'm missing something, but this looks like it is taking the address of the foo
function, casting it to an int
, and using that int
as the first argument (num
) to calloc
. This will most likely allocate a large amount of memory, rather than just enough for 23 elements that you think it's allocating, which explains why the fread
does not cause any errors.
Upvotes: 4