Function pointer ambiguity in C ++

I have two questions:

Q1) Are function names themselves pointers?

If these are pointers, then what values ​​are stored in them?

Otherwise, if they are not pointers, then what are they and what values ​​are stored in them?

If we assume that function names are pointers. Then:

void display(){...} int main () { void (*p)(); **p=display; //Works (justified**, because we are assigning one pointer into another) **p=&display; //Works (Not justified** If function name is a pointer (let say type*) , then &display is of datatype : type**. Then how can we assign type** (ie &display) into type * (ie p)??) **p=*display; //Works (Not justified** If function name is a pointer ( type *) ,then, how can we assign type (ie *display) into type * (ie p) ?? ) } 

Yet again,

cout<<display<<";"<<&display<<";"<<*display;

Prints something like:

0x1234; 0x1234; 0x1234

[1234 is just an example]

[OMG! How is this possible? How to indicate the address of the pointer, the address to which it points, and the value at the specified address are all the same? ]

Q2) What value is stored in a user-defined function pointer?

Consider an example:

 void display(){...} int main() { void (*f)(); f=display; f=*f; // Why does it work?? How can we assign type (ie *f ) into type * (ie f). cout<<f<<";"<<&f<<";"<<*f; //Prints something like : 0x1234;0x6789;0x1234 } 

[The first two outputs are justified ... But how can the value in the pointer (the address it points to) be equal to the value stored at the specified address? ]

Again:

 f=*********f; // How can this work? 

I searched it on the Internet, but all the information that is available is about using and example code to create function pointers. None of them says that they are, and how they differ from ordinary pointers.

So, I have to miss something very basic. Please indicate to me what I am missing. (Sorry for my ignorance becoming a newbie.)

+5
source share
1 answer

Are function name names themselves?

No. However, in some contexts, a function can be automatically converted to a pointer to a function. The standard says in paragraph 4.3:

The value of the type of the function T can be converted to a prvalue of type "pointer to T". The result is a pointer to a function.

(The name of the function means lvalue, but there may be other lvalues ​​of type of the function).

In your examples

 p = display; p = *p; 

there is just this type of automatic conversion. display and *p are l-values ​​of the type of the function, and if necessary, they are silently and automatically converted to the type of a pointer to a function.

 p = *display; 

Here, the conversion occurs twice: first, the display converted to a pointer to the * operator, then it is dereferenced, and then converted to the pointer to the = operator again.

 cout << display << ";" << &display << ";" << *display; 

Here display converted to a pointer to operator << ; &display already a pointer, because & is the usual addressing operator; and *display converted to a pointer to operator << , and inside it, display converted to a pointer to operator * .

 f = *********f; 

There are many transformations of this kind in this expression. Collect them yourself!

+10
source

Source: https://habr.com/ru/post/1262601/


All Articles