C++ Pointers
C++ Pointers
Definition
incremented ( ++ )
decremented ( -- )
an integer may be added to a pointer ( + or += )
an integer may be subtracted from a pointer ( – or -= )
Pointer arithmetic is meaningless unless performed on an array.
Note : Pointers contain addresses. Adding two addresses makes no
sense, because there is no idea what it would point to. Subtracting two
addresses lets you compute the offset between these two addresses.
int main()
{
int v[3] = { 10, 100, 200 };
// Declare pointer variable
int* ptr;
// Assign the address of v[0] to ptr
ptr = v;
for (int i = 0; i < 3; i++)
{
cout << "Value of *ptr = " << *ptr << endl;
cout << "Value of ptr = " << ptr << endl;
// Increment pointer ptr by 1
ptr++;
}
}
Array Name as Pointers
An array name acts like a pointer constant. The value of this pointer
constant is the address of the first element.
For example, if we have an array named val
then val and &val[0] can be used interchangeably.
new and delete Operators For Dynamic
Memory