difference between function pointers in c and c++ [duplicate] - c++

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to print function pointers with cout?
What is the difference between function pointers in c and c++? When I'm printing function pointer in c++ it's giving 1, but in c it's giving an address.
#include <iostream>
int fun()
{}
typedef int (*f)();
int main()
{
f test = fun;
std::cout << reinterpret_cast<f>(test);
}
#include <stdio.h>
int fun()
{}
int (*f)();
int main()
{
f = fun;
printf("%p", f);
}

The function pointer can't convert to void*, so in program using C++ i/o it converts to bool. The C program is also a C++ program. You should get no difference when you compile it as C++.
Cheers & hth.,

The main difference is the printing mechanism you're using. std::cout and printf have different semantics. std::cout refers to your pointer as boolean data and prints either 0 or 1 instead of the address.

The real question is what's the difference between printf with "%p" and cout when you stream a pointer to a function.
To print out the pointer to function in C++, try casting it to a char* and iterating through the bytes (with length of sizeof(test)). I think you'll find it to be similar to "%p"

Related

c++ function pointer evaluates to 1 [duplicate]

This question already has answers here:
How to print function pointers with cout?
(7 answers)
Closed 5 years ago.
I define a simple function pointer pointing to a function, and when i try to output it evaluate to 1. What is happening behind the scene? (I am on mac, compiling with c++11 g++ compiler)
#include <iostream>
int foo()
{
return 5;
}
int main(int argc, char const *argv[])
{
int (*fcptr)() = foo;
std::cout<< fcptr;
return 0;
}
Output is 1.
There is no overload of operator<< which takes std::ostream and a function pointer. However, there is one that takes std::ostream and a bool, and there is implicit conversion from function pointers to bool.
So your code converts the function pointer to bool, which is defined as yielding true if it was not a null pointer; and then outputs true , which is defined as outputting 1 by default. You could do std::cout<< std::boolalpha << fcptr; to see true outputted.

Unexpected behaviour of std::cout [duplicate]

This question already has answers here:
How to print function pointers with cout?
(7 answers)
Closed 5 years ago.
I was playing around with function pointers recently, when I discovered that the value of the function pointer as printed by std::cout always evaluates to 1.
However that was not the case with printf(), and it prints the expected result.
It'd be great if someone could explain the reason behind such behavior.
Below is the code sample for reference.
#include<iostream>
using namespace std;
int fun(int a)
{
return 0;
}
int main()
{
cout<<(&fun)<<endl; //always prints 1
printf("%u",&fun); //this prints the expected result
return 0;
}
The printf call is simply undefined behavior. A function pointer is not an unsigned integer, and thus supplying it for a %u argument is not valid. (Try running this code on a 64-bit platform. You won't get the correct function pointer value.)
The cout on the other hand is type-safe. The compiler sees a function pointer argument and tries to find the best printing function (operator<< overload) it can. There is no such overload for function pointers themselves and the pointers don't offer a lot of implicit conversions. There is just one overload that works, and that is the one for bool. So the compiler converts the non-NULL function pointer to true and passes that. The overload then prints this as 1, but you could use the std::boolalpha modifier to make it print as true instead.
Your cout treats (&fun) as a boolean expression and warns that it will always evaluate to true (i.e. non-zero).
Try casting it to void* as addresses should be printed, and check what happens:
#include <iostream>
#include <stdio.h>
using namespace std;
int fun(int a){
return 0;
}
int main(){
cout<<(void*)(&fun)<<endl; //always prints 1
/* Side note: This will print the same as above */
// cout<<(void*)(fun)<<endl; // note the missing &
printf("%p",(void*)&fun); //this prints the expected result
// while(1);
return 0;
}
output on mu machine:
0x401460
0x401460

what's the difference between these two pointer code [duplicate]

This question already has answers here:
Pointer vs. Reference
(12 answers)
Closed 6 years ago.
I found these two different sources, but they do the exact same thing. I was wondering if there is a difference or not but I couldn't figure it out.
Can anyone tell me the difference and when should I use which?
this is the first one:
void function1(int *x) {
*x = 100;
}
int main() {
int var1 = 10;
function1(&var1);
cout << var1 << endl;
}
and this is the second one:
void function2(int &x) {
x = 100;
}
int main() {
int var2 = 10;
function2(var2);
cout << var2 << endl;
}
int *x is a pointer whereas int &x is a reference. Probably the biggest difference is that you can't change where reference is pointing to.
The first is a pointer, the second is a reference. The ideas have some similarities, but there are also differences.
A pointer is a C and C++ mechanism and a bit more "pure" but gives you more posibilies for advanced concepts like pointer arithmetics. References are C++ only and are more safe and more implicit, as a reference is used with the same syntax as a normal varible while using the referenced one. A pointer is more explicit if you want to use or change its value, as you have to explicitely dereference it using *var, and explicitely have obtain it.

why are these NOT overloading functions? [duplicate]

This question already has answers here:
Passing an array as an argument to a function in C
(11 answers)
Closed 7 years ago.
I read here:
C: differences between char pointer and array
that char pointers and char arrays are not the same. Therefore, I would expect these to be overloading functions:
#include <iostream>
using namespace std;
int function1(char* c)
{
cout << "received a pointer" << endl;
return 1;
}
int function1(char c[])
{
cout << "received an array" << endl;
return 1;
}
int main()
{
char a = 'a';
char* pa = &a;
char arr[1] = { 'b' };
function1(arr);
}
Yet upon building I get the error C2084: function 'int function1(char *)' already has a body. Why does the compiler seem to consider a char pointer to be the same as a char array?
Because when you pass an array into a function, it magically becomes a pointer.
Your two functions, then, are the same.
The following are literally* identical:
void foo(int arr[42]);
void foo(int arr[]);
void foo(int* arr);
(* not lexically, of course :P)
This historical C oddity is the major reason lots of people mistakenly think that "arrays are pointers". They're not: this is just a bit of an edge case that causes confusion.

C++ passing/accessing matrix by pointers [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
2D arrays with C++
Hi, I'm trying to copy a pointer to a matrix that i'm passing in to a function in C++. here's what my code is trying to express
#include <iostream>
using namespace std;
void func( char** p )
{
char** copy = p;
cout << **copy;
}
int main()
{
char x[5][5];
x[0][0] = 'H';
func( (char**) &x);
return 0;
}
However, this gives me a Seg Fault. Would someone please explain (preferrably in some detail) what underlying mechanism i'm missing out on? (and the fix for it)
Thanks much in advance :)
A pointer to an array of 5 arrays of 5 char (char x[5][5]) has the type "pointer to array of 5 arrays of 5 chars", that is char(*p)[5][5]. The type char** has nothing to do with this.
#include <iostream>
using namespace std;
void func( char (*p)[5][5] )
{
char (*copy)[5][5] = p;
cout << (*copy)[0][0];
}
int main()
{
char x[5][5];
x[0][0] = 'H';
func(&x);
return 0;
}
Of course there are many other ways to pass a 2D array by reference or pointer, as already mentioned in comments. The most in-detail reference is probably StackOverflow's own C++ FAQ, How do I use arrays in C++?
char** is a pointer to a pointer (or an array of pointers). &x is not one of those - it's a pointer to a two-dimensional array of chars, which can be implicitly converted to a pointer to a single char (char *). The compiler probably gave you an error, at which point you put in the cast, but the compiler was trying to tell you something important.
Try this instead of using a char**:
#include <iostream>
using namespace std;
void func( char* &p )
{
char* copy = p;
cout << copy[0];
}
int main()
{
char x[5][5];
x[0][0] = 'H';
func(&x[0]);
return 0;
}