When I use auto
to deduce a pointer type, I found a weird phenomenon. My code is like this:
#include <iostream>
using namespace std;
int main()
{
int i = 100;
auto p1 = &i;
auto *p2 = &i;
cout << *p1 << " " << *p2 << endl;
return 0;
}
After compiling and executing, we can find that the result of *p1
and *p2
is the same, both 100. This means p1
and p2
are both a pointer object which points to an int
object.
[user@host ~]$ ./test
100 100
Is there any difference between these two statements which define p1
and p2
?