OOP Lecture 3
OOP Lecture 3
#include <iostream>
using namespace std;
int main()
{ int arr[5] = {5, 2, 9, 4, 1};
int *ptr = &arr[2];
cout<<"The value in the second index of the array is: "<< *ptr;
return 0; }
A single pointer is used to access all the elements of the array is given as
follows.
#include <iostream>
using namespace std;
int main()
{ int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[0];
cout<<"The values in the array are: ";
for(int i = 0; i < 5; i++)
{ cout<< *ptr <<" ";
ptr++;
}
return 0;
}
SOME COMMANDS AND THEIR
MEANINGS
Int * p1 // Pointer declared
p1 =&firstvalue;// pointer points to first value
P1= var1// error as pointer can point to address or *p1 can access the value of the
variable
P1=& var 1// copies address of variable to the pointer.
*p1=10// stores the given value in pointer that is pointing to ?
Cout<< p1// Always displays address
Cout<<*p1 always displays value(pointed value or stored value)
*p1 = 10; // value pointed to by p1 = 10
p1 =&firstvalue;
Cout<<*p1 the value stored in the pointer is displayed.i.e.
POINTERS AND ARRAYS
The concept of arrays is related to that of pointers. In fact, arrays work very much like
pointers to their first elements, and, actually, an array can always be implicitly
converted to the pointer of the proper type. For example, consider these two
declarations:
int myarray [20];
int * mypointer;
The following assignment operation would be valid:
mypointer = myarray;
The main difference is
that mypointer can be assigned a
different address,
whereas myarray can never be
assigned anything, and will
always represent the same
block of 20 elements of
type int.
Therefore, the following assignment would not be valid:
myarray=mypointer;
a[5] = 0;
*(a+5) = 0;
POINTER ARITHMETICS
To conduct arithmetical operations on pointers is a little different than to conduct them on
regular integer types. To begin with, only addition and subtraction operations are allowed; the
others make no sense in the world of pointers. But both addition and subtraction have a slightly
different behavior with pointers, according to the size of the data type to which they point.
When fundamental data types were introduced, we saw that types have different sizes.
For example: char always has a size of 1 byte, short is generally larger than that,
and int and long are even larger; the exact size of these being dependent on the system. For
example, let's imagine that in a given system, char takes 1 byte, short takes 2 bytes,
and long takes 4.
EXAMPLE
Suppose now that we define three pointers in this compiler:
char *mychar; short *myshort; long *mylong;
and that we know that they point to the memory locations 1000, 2000, and 3000,
respectively.
++mychar; ++myshort; ++mylong;
mychar, as one would expect, would contain the value 1001. But not so
obviously, myshort would contain the value 2002, and mylong would
contain 3004, even though they have each been incremented
only once. The reason is that, when adding one to a pointer, the
pointer is made to point to the following element of the same
type, and, therefore, the size in bytes of the type it points to is
added to the pointer.
POINTERS AND CONSTANTS
Pointers can be used to access a variable by its address, and this access may include
modifying the value pointed. But it is also possible to declare pointers that can access
the pointed value to read it, but not to modify it. For this, it is enough with qualifying
the type pointed to by the pointer as const. For example:
int x;
int y = 10;
const int* p;// = &y;
p = &y;
x = *p; // ok:
reading p
cout << x;
//*p = x; //
error: modifying p, which
is const-qualified
return 0;