I am writing simple code of pointer. But i m comfused here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include<iostream>
usingnamespace std;
int main()
{
int i = 5;
int *ip1 = &i;
int *ip2;
ip2 = &i;
cout << *ip1 <<endl;
cout << *ip2 << endl;
}
In above code 1st I assigned "*ip1 = &i" and after that "ip2 = &i".
when i print *ip1 and *ip2 then they both print same values.
I m confused here. Assigning ADDRESS to *ptr1 & ADDRESS to ptr2 doesnt make any difference while printing both.
That is because they both point to the same value. ip1 is a pointer to i, and so is ip2. You'd notice that if you changed the value of i later, then both ip1 and ip2 would print a value relevant to i. This is just the way pointers work.
Its because you are dereferencing ip1 when you are assigning it to the address of i. The error is saying it cannot change from a pointer address (&i) to a plain integer (the value pointed to by ip1). Instead, you probably want to do this:
1 2 3
int i = 5;
int *ip1;
ip1 = &i;
Then, if you want to set the value pointed to by ip1, you dereference it first, like this:
*ip1 = 6;
It would probably be worth your while to find a few tutorials on pointers and read them a few times (sometimes it helps to get a different look at things).