Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of int (*) (int *) = 5 (or any integer value)

I cannot figure this out:

int main() {     int (*) (int *) = 5;     return 0; } 

The above assignment compiles with g++ c++11. I know that int (*) (int *) is a pointer to a function that accepts an (int *) as argument and returns an int, but I do not understand how you could equate it to 5. At first I thought it is a function that constantly returns 5 (from my recent learning in F#, probably, haha), then I thought, briefly, that the function pointer points to memory location 5, but that does not work, clearly, and neither does hex values.

Thinking that it could be because the function returns an int, and that assigning an int is ok (somehow), I also tried this:

int * (*) (int *) = my_ptr 

where my_ptr is of type int *, the same type as this second function pointer, as in the first case with type int. This does not compile. Assigning 5, or any int value, instead of my_ptr, doesn't compile for this function pointer either.

So what does the assignment mean?

Update 1

We have confirmation that it is a bug, as shown in the best answer. However, it is still not known what actually happens to the value that you assign to the function pointer, or what happens with the assignment. Any (good) explanations on that would be very much appreciated! Please refer to the edits below for more clarity on the problem.

Edit 1

I am using gcc version 4.8.2 (in Ubuntu 4.8.2)

Edit 2

Actually, equating it to anything works on my compiler. Even equating it to a std::string variable, or a function name that returns a double, works.

Edit 2.1

Interestingly, making it a function pointer to any function that returns a data type that is not a pointer, will let it compile, such as

std::string (*) () = 5.6; 

But as soon as the function pointer is to a function that returns some pointer, it does not compile, such as with

some_data_type ** (*) () = any_value; 
like image 343
Konrad Avatar asked Apr 14 '15 20:04

Konrad


People also ask

What does int * A 5 means?

int *a[5] - It means that "a" is an array of pointers i.e. each member in the array "a" is a pointer. of type integer; Each member of the array can hold the address of an integer. int (*a)[5] - Here "a" is a pointer to the array of 5 integers, in other words "a" points to an array that holds 5 integers.

What is the meaning of int (* a 3?

The int *(a[3]) is the same as plain int *a[3] . The braces are redundant. It is an array of 3 pointers to int and you said you know what it means. The int (*a)[3] is a pointer to an array of 3 int (i.e. a pointer to int[3] type). The braces in this case are important.

What is difference between int * a and int * A?

There is no such difference in between these two types of array declaration. It's just what you prefer to use, both are integer type arrays. There is no difference in functionality between both styles of declaration. Both declare array of int.

What does int mean in C?

Updated on January 07, 2019. Int, short for "integer," is a fundamental variable type built into the compiler and used to define numeric variables holding whole numbers. Other data types include float and double. C, C++, C# and many other programming languages recognize int as a data type.


2 Answers

It's a bug in g++.

 int (*) (int *)  

is a type name.

In C++ you cannot have a declaration with a type name without an identifier.

So this compiles with g++.

 int (*) (int *) = 5; 

and this compiles as well:

 int (*) (int *); 

but they are both invalid declarations.

EDIT:

T.C. mentions in the comments bugzilla bug 60680 with a similar test case but it has not yet been approved. The bug is confirmed in bugzilla.

EDIT2:

When the two declarations above are at file scope g++ correctly issues a diagnostic (it fails to issue the diagnostic at block scope).

EDIT3:

I checked and I can reproduce the issue on the latest release of g++ version 4 (4.9.2), latest pre-release version 5 (5.0.1 20150412) and latest experimental version 6 (6.0.0 20150412).

like image 147
ouah Avatar answered Oct 05 '22 16:10

ouah


It is not valid C++. Remember that because your particular compiler happens to compile it doesn't make it valid. Compilers, like all complex software, sometimes have bugs and this appears to be one.

By contrast clang++ complains:

funnycast.cpp:3:11: error: expected expression     int (*) (int *) = 5;           ^ funnycast.cpp:3:18: error: expected '(' for function-style cast or type construction     int (*) (int *) = 5;              ~~~ ^ funnycast.cpp:3:19: error: expected expression     int (*) (int *) = 5;                   ^ 3 errors generated. 

This is the expected behavior because the offending line is not valid C++. It purports to be an assignment (because of the =) but contains no identifier.

like image 21
Edward Avatar answered Oct 05 '22 17:10

Edward