CH 6
CH 6
POINTER
Out line
Pointer declaration
Pointer operation
Examples:
int *n;
double *r;
int **p; // pointer to pointer
int *p1, *p2, v1, v2; p1 and p2 point to variables of type int v1 and v2 are
variables of type int .
POINTER VARIABLES
Memory address: 1020 1024 1032
… 88 100 … 1024 …
a p
int a = 100; Result is:
int *p = &a; 100 1024
cout << a << " " << &a <<endl; 1024 1032
cout << p << " " << &p <<endl;
p1=&v1;
p1 is now a pointer to v1. v1 can be called "the variable pointed
by p1".
return 0;
}
Output of the above program:
value1==10/value2==20
ARITHMETIC OF POINTERS
Only addition and subtraction operations are allowed to be
conducted.
But both addition and subtraction have a different behavior with
pointers according to the size of the data type to which they point
to.
When we saw the different data types that exist, we saw that some
occupy more or less space than others in the memory.
For example, in the case of integer numbers, int occupies 4 byte,
char occupies 1 byte, short occupies 2 bytes and long occupies 4.
CONT…
Let's suppose that we have 3 pointers: char *mychar; short
*myshort; long *mylong; and let us assume that they point to
memory locations 1000, 2000 and 3000 respectively. So if we
write: mychar++;
myshort++;
mylong++;
mychar, as you may expect, would contain the value 1001.
myshort would contain the value 2002, and
mylong would contain 3004.
CONT…
EXAMPLE 4:
#include <iostream>
using namespace std;
int main()
{
short myshort1 = 2;
int mylong1 = 45;
short *myshort = &myshort1;
int *mylong = &mylong1;
cout << myshort << endl;
cout << mylong << endl;
myshort++;
mylong++;
cout << myshort << endl;
cout << mylong << endl;
return 0;
}
Output
0x6dfef6
0x6dfef0
0x6dfef8
0x6dfef4
CONT..
If the address of x is 2000, then after statement p=p-3; then the value of p is
1988 (as the size of int is 4, when we subtract 3 integers, we have to multiply it
with 4 which 4*3 = 12). Look the following example
#include<iostream>
using namespace std;
int main() {
float x=9,*p;
p=&x;
cout <<p<<endl;
p=p-3;
cout <<p;
return 0;
}
Output
0x6dfee8
0x6dfedc
Pointers to Pointers
A pointer to a value that is itself a pointer can be declared using
multiple levels of * symbols.
char a;
char *b;
char **c;
a='z';
b=&a;
c=&b;
Supposing random memory locations 7230, 8092 and 10502, could
be described thus:
void * Pointers
A void pointer can reference any type of value.
double d;
int f;
double * dp = & d; //legal
dp=&f; //illegal because dp is double whereas f is int.
void * p = dp; //legal, p can hold all data types
P=&d; //legal
p=&f; //legal
However, when we assign a void * parameter like p, it must always
be cast (changed) before it can be used:
double * dp2;
dp2 = (double *)p; // convert p back into pointer to double because p
is void * type.
Pointers and Arrays
The identifier of an array is equivalent to the address of its first
element.
int num [20];
int * p;
The following allocation would be valid:
p = num;
you can also say:
p=&num[0];
At this point p and numbers are equivalent. The only difference
that we could assign another value to the pointer p whereas
numbers will always point to the first of the 20 integer numbers of
type int with which it was defined. So, unlike p, that is an ordinary
variable pointer, numbers is a constant pointer (indeed that is an
Array: a constant pointer). The following allocation is not valid:
numbers = p; because numbers is an array (constant pointer), and no
values can be assigned to constant identifiers.
EXAMPLE:
#include<iostream>
using namespace std;
int main() {
int numbers[5];
int * p;
p = numbers;
*p = 10;
p++;
*p = 20;
p = &numbers[2];
*p = 30;
p = numbers + 3;
*p = 40;
p = numbers;
*(p+4) = 50;
for(int n=0; n<5; n++)
cout <<numbers[n]<<",";
}
Output of the above program:
10, 20, 30, 40, 50,
EXAMPLE 2:
#include<iostream>
using namespace std;
int main() {
int a[5]={5,6,3,2,9};
int *p;
p=&a[0];
for(int i=0;i<=4;i++)
cout <<*(p+i)<<" ";
return 0;
}
Output of the above program:
56329
Array name is pointer. Therefore, the above program can be written as:
#include<iostream>
using namespace std;
int main() {
int a[5]={5,6,3,2,9};
for(int i=0;i<=4;i++)
cout <<*(a+i)<<" ";
return 0;
}
Pointer and String
We know strings are array of characters. Therefore, pointer
notation can be applied to the characters in strings.
But in the case of string if we initialize the string at the time of
declaration, the string name is constant.
Therefore, we cannot perform pointer arithmetic operation with
that.
See the following example:
#include<iostream>
using namespace std;
int main() {
char str[]="dawed nesru";
cout <<str;
str++; //illegal
return 0;
}
The program does not have an output because of line 6:
#include<iostream>
using namespace std;
int main() {
char *str=“abebe maru";
cout <<str<<endl;
str++;
cout <<str;
return 0;
}
Output of the above program:
Abebe maru
bebe maru
POINTER AND STRUCTURE
READING ASSIGNMENT ???
U
Y O
N K
A Q ?
T H