#include<iostream>
void main()
{usingnamespace std;
int iaArray[] = {1,2,3,4,5};
int iAnotherInt = 20;
int*ipPtr = 0;
int iInt = 10;
int *iAnotherpPtr = &iAnotherInt;
ipPtr = &(iaArray[0]);
cout << "this is first number in the array: " << *ipPtr << endl;
++ipPtr;/* this tells the *ipPtr array to access the next number in the array*/
cout << "this is second number in the array (because the ++ adds from first array value in array[1] which makes/2/): " << *ipPtr << endl;
--ipPtr;/* this tells the *ipPtr array to access the next number in the array*/
cout << "this is first number in the array (because the -- subtracts from the second value in the array [2] which makes /1/): " << *ipPtr << endl;
ipPtr = ipPtr + 3;/* this add 3 from the previous reference in the ipPtr array*/
cout << "this is the fourth number in the array (because adding 3 from the first array address points to the forth array adress [4]): " << *ipPtr << endl;
ipPtr = &iAnotherInt;/*this changes the value ipPtr is pointing to*/
cout << endl <<endl <<endl <<endl <<endl <<endl <<endl << "Changing pointer value" <<endl << "ipPtr is now pointing to another value/20/: " << *ipPtr << endl;
*ipPtr = iInt;
cout << "this time ipPtr points to an interger iInt (which is 10): " <<endl;
//int **ipPtr = &iAnotherpPtr; /*a pointer now points to another pointer*/
//cout << "*ipPtr points to iAnotherpPtr (which has the value 20): " << *ipPtr << endl;
system("PAUSE");
}
You have two variables with the same name.int *ipPtr and int **ipPtr. And you need two dereferencing stars to print out the value of the second ipPtr on line 31.
#include<iostream>
void main()
{usingnamespace std;
int iaArray[] = {1,2,3,4,5};
int iAnotherInt = 20;
int*ipPtr = 0;
int iInt = 10;
int *iAnotherpPtr = &iAnotherInt;
ipPtr = &(iaArray[0]);
cout << "this is first number in the array: " << *ipPtr << endl;
++ipPtr;/* this tells the *ipPtr array to access the next number in the array*/
cout << "this is second number in the array (because the ++ adds from first array value in array[1] which makes/2/): " << *ipPtr << endl;
--ipPtr;/* this tells the *ipPtr array to access the next number in the array*/
cout << "this is first number in the array (because the -- subtracts from the second value in the array [2] which makes /1/): " << *ipPtr << endl;
ipPtr = ipPtr + 3;/* this add 3 from the previous reference in the ipPtr array*/
cout << "this is the fourth number in the array (because adding 3 from the first array address points to the forth array adress [4]): " << *ipPtr << endl;
ipPtr = &iAnotherInt;/*this changes the value ipPtr is pointing to*/
cout << endl <<endl <<endl <<endl <<endl <<endl <<endl << "Changing pointer value" <<endl << "ipPtr is now pointing to another value/20/: " << *ipPtr << endl;
*ipPtr = iInt;
cout << "this time ipPtr points to an interger iInt (which is 10): " <<endl;
**ipPtr = &iAnotherpPtr; /*a pointer now points to another pointer*/
cout << "*ipPtr points to iAnotherpPtr (which has the value 20): " << *ipPtr << endl;
system("PAUSE");
}
Because in your file ipPtr is an int*, a pointer to an int
You use it as if it were a pointer to an int* (an int**)
Especially this **ipPtr, ipPtr is int* not int**
If you want to point to a pointer that points to an int (int**), you cant use int*
1 2 3 4 5
int** ipPtrPtr = &iAnotherpPtr;
// ipPtrPtr = &iAnotherpPtr = int**
// *ipPtrPtr = iAnotherPtr = &iAnotherInt = int*
//**ipPtrPtr = *iAnotherPtr = iAnotherInt = 20 = int
cout << "ipPtr points to &iAnotherpPtr (which points to iAnother int which has the value 20): " << **ipPtr << endl;
Pointers can be very hard to understand if youre new to them, especially if you mix them with arrays =P
#include<iostream>
void main()
{usingnamespace std;
int iaArray[] = {1,2,3,4,5};
int iAnotherInt = 20;
int*ipPtr = 0;
int iInt = 10;
int *iAnotherpPtr = &iAnotherInt;
ipPtr = &(iaArray[0]);
cout << "this is first number in the array: " << *ipPtr << endl;
++ipPtr;/* this tells the *ipPtr array to access the next number in the array*/
cout << "this is second number in the array (because the ++ adds from first array value in array[1] which makes/2/): " << *ipPtr << endl;
--ipPtr;/* this tells the *ipPtr array to access the next number in the array*/
cout << "this is first number in the array (because the -- subtracts from the second value in the array [2] which makes /1/): " << *ipPtr << endl;
ipPtr = ipPtr + 3;/* this add 3 from the previous reference in the ipPtr array*/
cout << "this is the fourth number in the array (because adding 3 from the first array address points to the forth array adress [4]): " << *ipPtr << endl;
ipPtr = &iAnotherInt;/*this changes the value ipPtr is pointing to*/
cout << endl <<endl <<endl <<endl <<endl <<endl <<endl << "Changing pointer value" <<endl << "ipPtr is now pointing to another value/20/: " << *ipPtr << endl;
*ipPtr = iInt;
cout << "this time ipPtr points to an interger iInt (which is 10): " <<*ipPtr <<endl;
/**ipPtr = &iAnotherpPtr; /*a pointer now points to another pointer*/
/*cout << "*ipPtr points to iAnotherpPtr (which has the value 20): " << *ipPtr << endl;*/
//BUY SKILLESS programer c++.com
int** ipPtrPtr = &iAnotherpPtr;
// ipPtrPtr = &iAnotherpPtr = int**
// *ipPtrPtr = iAnotherPtr = &iAnotherInt = int*
//**ipPtrPtr = *iAnotherPtr = iAnotherInt = 20 = int
cout << "ipPtr points to &iAnotherpPtr (which points to iAnother int which has the value 20): " << **ipPtr << endl;
system("PAUSE");
}
Id love to compile it but im at work right now so, sorry, i cant compile here =P
Also,
1 2
*ipPtr = iInt;
cout << "this time ipPtr points to an interger iInt (which is 10): " <<*ipPtr <<endl;
Youre saying it points to another int, but youre actually changing the value of iAnotherInt to 10 *ipPtr = iInt; is the same as doing iAnotherInt = iInt
This also means that you shouldnt expect 20 at the end, but 10, because iAnotherInt has been set to 10!