Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function Pointer ambiguity in C++

I have two questions :

Q1) Are the function names themselves pointers ??

If they are pointers , then what values are stored in them?

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

If we consider 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** (i.e. &display) into type * (i.e. p)??)

    **p=*display; //Works (Not justified** If function name is a pointer ( type *) ,then, how can we assign type (i.e. *display) into type * (i.e. p) ?? )
}

Again ,

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

Prints something like :

0x1234;0x1234;0x1234

[1234 is just for example]

[OMG! How is this possible ??How can address of a pointer, address it is pointing to and the value at pointed address all be same?]

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

Consider the example :

void display(){...}

int main()
{
    void (*f)();

    f=display;

    f=*f; // Why does it work?? How can we assign type (i.e. *f ) into type * (i.e.  f). 

    cout<<f<<";"<<&f<<";"<<*f;

    //Prints something like :

    0x1234;0x6789;0x1234
}

[First two outputs are justified... But how can the value in a pointer (address it is pointing to) be equal to the value stored in the pointed address? ]

Again :

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

I searched it online but all info that is available is regarding usage and example code to create function pointers. None of them say about what they are or how they are different from normal pointers.

So I must be missing something very basic thing. Please point me out what I am missing. (Sorry for my ignorance being a beginner.)

like image 287
Madhuchhanda Mandal Avatar asked Jan 10 '17 19:01

Madhuchhanda Mandal


People also ask

What is functional pointer in C?

Function pointers in C are variables that can store the memory address of functions and can be used in a program to create a function call to functions pointed by them.

What does * and& indicate in pointer?

The fundamental rules of pointer operators are: The * operator turns a value of type pointer to T into a variable of type T . The & operator turns a variable of type T into a value of type pointer to T .

Can function pointers be null?

Can I always reliably set a function pointer to NULL in C and C++? Yes, in any conforming C or C++ implementation this will work.

What is function pointer and its advantages?

1) Unlike normal pointers, a function pointer points to code, not data. Typically a function pointer stores the start of executable code. 2) Unlike normal pointers, we do not allocate de-allocate memory using function pointers. 3) A function's name can also be used to get functions' address.


1 Answers

Are the function names themselves pointers?

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

An lvalue of function type T can be converted to a prvalue of type “pointer to T.” The result is a pointer to the function.

(A function name designates an lvalue, but there can be other lvalues of function type).

In your examples

p = display;
p = *p;

there's exactly this kind of automatic conversion. display and *p are lvalues of a function type, and when needed, they are silently and automatically converted to a pointer-to-function type.

p = *display; 

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

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

Here, display is converted to a pointer for operator <<; &display is already a pointer because & is a normal address-taking operator; and *display is converted to a pointer for operator << while inside it display is converted to a pointer for operator *.

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

There are many conversions of this kind in this expression. Count them yourself!

like image 59
n. 1.8e9-where's-my-share m. Avatar answered Sep 27 '22 01:09

n. 1.8e9-where's-my-share m.