1

I have initialized a structure of static with function name as shown below.

We have to initialize a static structure with constants. Is function names are constants in C?

struct fp {
        int (*fn)();
};
int f1()
{
        printf("f1 called \n");
        return 0;
}
static struct fp fps = {
        .fn = f1,
};
int main()
{
        fps.fn();
        return 0;
}

If is compiling without any issues when initialized the structure as shown below.

static struct fp fps = {
        .fn = &f1,
};

In C for a function name both f1 and &f1 are same?

user3693586
  • 1,227
  • 5
  • 18
  • 40
  • 1
    What error do you get? – Some programmer dude Jun 04 '15 at 11:32
  • 1
    What is your question? I don't understand the last line. – moffeltje Jun 04 '15 at 11:34
  • He is asking whether `&func` is the same as `func`. And the answer is yes, they're the same, pick whichever you like better. See here: http://stackoverflow.com/questions/840501/how-do-function-pointers-in-c-work – user4520 Jun 04 '15 at 11:35
  • I am not getting any error. My doubht here is - if we are initializing a static object then the initialization elements should be constans. Is functions are constants in C? – user3693586 Jun 04 '15 at 11:36
  • Functions aren't constants (they're not variables). Functions are functions. They have their own place in the grand scheme. – Kerrek SB Jun 04 '15 at 11:38
  • If i compile the following program it will throw " initializer element is not constant" error, because here x is a variable. struct ex { int a; }; int main() { int x= 4; static struct ex sample = { .a = x, }; return 0; } similarly if i assign a function during structure initialization it is not giving any compilation error, why? – user3693586 Jun 04 '15 at 11:48
  • @user3693586 The function is as unchangeable as `1` or `'A'` are. You will get the error if you first assign the function pointer to a variable and then use the variable for initialization (like you do with the `int`). – molbdnilo Jun 04 '15 at 12:12
  • so compiler will treat function name as constant. – user3693586 Jun 04 '15 at 12:15

1 Answers1

3

f1 is the name of a function. In most contexts, when used as an expression, the function name decays automatically to a pointer to the function. One context where the decay doesn't happen is when the function name is the operand of the address-of operator (&). So as evaluated expressions, f1 and &f1 are usually the same.

In fact, in any function call expression f(args), f is a pointer to a function. The reason that you can call functions simply by their name is because of the automatic decay from function name to function pointer.

You can turns this around and dereference function pointers, too. They'll decay back to a pointer right away:

int f(void);

f();         // "f" decays to &f
(&f)();      // normal call via function pointer, no implicit decay
(*f)();      // why not
(*****f)();  // ditto
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • Thanks for the info. My initial doubht is, If i compile the following program then it wil throw "initializer element is not constant" error, because x is a variable. struct ex { int a; }; int main() { int x= 4; static struct ex sample = { .a = &x, }; return 0; } similarly if we assign function name during initialization then it is not throwing any error. why? – user3693586 Jun 04 '15 at 12:08
  • @user3693586: Ah, right. Static initializers have to be compile-time constant expressions. I imagine that addresses of functions are such (though I don't know the relevant standardese). – Kerrek SB Jun 04 '15 at 16:28