Invalid conversion from int to int*

I'm using a MinGW compiler on Windows XP.

Would anyone know why this would cause an error.
The error is: 'Invalid conversion from int to int*.

in my main function...

int *x = 5;

Yes, I know it's simple, but that's it.. Why would I get this error?

Thanks
you are declaring an x variable which has a time of "pointer to int" (int*)
Then you are trying to assing integer (int) value 5

Type mismatch.
I'm sorry but I don't understand your reply.
I didn't know that I could not create a pointer to an integer like this.
Could you reexplain the above, I'm just now sure what you're telling me. Thanks

1
2
3
4
5
6
7
8
9
10
11
//Instead I've done it like this.

int p = 5;

int *x = &p;

//Or, another way I can write this is:

int p = 5;
int *x;
x = &p;   

Last edited on
int* x is a pointer.
Pointers store memory addresses.

What does it mean to store "5" as a memory address?

If you want an integer variable, use int x = 5;.

If you want a pointer to a variable whose value is 5, use either
1
2
int someOtherInt = 5;
int* x = &someOtherInt;
or int* x = new int(5); (but if you do that last one, you have to delete x; after you're done).

If you want a pointer to the memory address 0x5 (not sure why -- that's not a spot you should be tampering with), try int* x = reinterpret_cast<int*>(5);.
So if I understand correctly the compiler will see the 5 as a memory location which is why I need to initiate int pointers differently.

Is that correct?

Thanks for your response. That's a good explanation.
Topic archived. No new replies allowed.