Home > Enterprise >  No Function to Pointer Decay during Assignment
No Function to Pointer Decay during Assignment

Time:01-06

In the below given code snippet, when i wrote f = A; then why doesn't A decay to a pointer to a function?

//Func is alias for "pointer to a function that returns an int and does not take any parameter"
typedef int (*Func)();

int A(){
    return 1;
}
int main()
{
    Func* f = &A;//cannot convert ‘int (*)()’ to ‘int (**)()’ in initialization - I UNDERSTAND THIS ERROR
   
   
    f = A;//error: cannot convert ‘int()’ to ‘int (**)()’ in assignment - My QUESTION IS THAT IN THIS CASE WHY DOESN'T "A" decay to a pointer to a function and give the same error as the above 
    
}

I know why Func *f = &A; produces error. But i expected f = A; to produce the same error because i thought in this case A will decay to a pointer to a function and hence should produce the same error as Func*f = &A;. To be precise, i thought i would get the error

error: cannot convert ‘int(*)()’ to ‘int (**)()’ in assignment

But to my surprise, there is no decay and i do not get the above error.

Why/How is this so? That is, why is there no decay.

CodePudding user response:

why doesn't A decay to a pointer to a function?

The error message says that a function (int()) cannot be implicitly converted to a pointer to a pointer to a function (int (**)()), because the type of the expression (A) is a function.

The function would decay if there was a valid conversion sequence to the target type through the decayed type. But there isn't such conversion sequence, and so the program is ill-formed.

  •  Tags:  
  • Related