invalid conversion & operator

Hello,
I think I got a quite easy problem. I am learning C++ with the tutorial on your website about pointers.

http://www.cplusplus.com/doc/tutorial/pointers/ .

My problem is when I take the codeexample with andy and fred and put it into a simple environment:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int main()
{
int andy;
int fred;
int ted;
 
andy = 25;
fred = andy;
ted = &andy;   
    
cout << 2 << endl;
    system("PAUSE");
}


I got the error mesage :

invalid code conversion from 'int' to '*int'.

When I initialize ted as a pointer it works. But why can't I store the adress of andy into an ordinary int variable?

I found the error message at google but in most cases in more complex problems.
I am curious for your answer.

Thanks for your help.
Fox123


Last edited on
It's just the compiler trying to stop you from making a mistake.
1
2
3
4
5
// you can cast your way out of it, although you shouldn't

ted = reinterpret_cast<int> (&andy); // obvious C++ cast
ted = (int)&andy; // evil C cast
ted = int(&andy); // evil alien C++ cast 


Please store addresses in pointers, like you're supposed to.
Topic archived. No new replies allowed.