n00b
n00b

Reputation: 336

Calling a function in C without parentheses that works, how?

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

Answers (1)

Tim Johns
Tim Johns

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

Related Questions