You are on page 1of 27

Chapter 4

Pointer
Pointer?
A pointer is a variable that holds the address of another variable.

A pointer variable is a variable whose value is the address of a location in memory.

For example, if one variable contains the address of another variable, the first variable is
int num=10;
said to point to the second. int *ptr;
Ptr=#

A pointer of type integer can hold the address of a variable of type integer.

A pointer of character type can hold the address of a variable of character type.

The base type of the pointer defines what type of variables the pointer can point to.
Addresses(&)
• To understand C++ pointers, you must understand how computers store data.

• When you create a variable in your C++ program, it is assigned some space the
computer memory. The value of this variable is stored in the assigned location.

• To know the location in the computer memory where the data is stored, C++
provides the & (reference) operator.

• The operator returns the address that a variable occupies.

• For example, if x is a variable, &x returns the address of the variable.


Example
int x;
float number;
char ch;

2000 2002 2006

x number ch
Example
The address of a variable can be obtained by using the address-of operator &.

int x;
float number; 2000 2002 2006
char ch; x number ch

cout << “Address of x is “ << &x << endl;

cout << “Address of number is “ << &number << endl;

cout << “Address of ch is “ << &ch << endl;

5
• Reference operator (&) and Deference operator (*)
• The reference operator (&) returns the variable’s address.
• & is the reference operator and can be read as "address of"

• The dereference operator (*) helps us to get the value that has been stored
in a memory address.
• * is the dereference operator and can be read as "value pointed by"
• For example: num 20 Num==20
int num=20; &num==x1780
x1780 Pt==x1780
int *pt;
*pt==20
pt=&num
Pointer declaration
•  Like other variable, you must declare a pointer before you can work with it.

• The general form(syntax) of a pointer variable declaration is

type *var-name int *ip; // pointer to an integer


double *dp; // pointer to a double
float *fp; // pointer to a float
char *ch // pointer to character

• type is the pointer's base type; it must be a valid C++ type. This type is not the
type of the pointer itself, but the type of the data the pointer points to.  

• Var-name is the name of the pointer variable.

• The asterisk is being used to designate a variable as a pointer.


• To declare a pointer variable, you must specify the type of value that the pointer
will point to, for example,
int* ptr; // ptr will hold the address of an int

char* q; // q will hold the address of a char

int *p1, *p2, i; // p1 and p2 are int pointers. i is an int


int* p1, p2, i; // p1 is an int pointer, p2 and i are int
int * p1, * p2, i; // p1 and p2 are int pointers, i is an int

• Naming Convention of Pointers: Include a "p" or "ptr" as prefix or suffix,


• e.g., iPtr, numberPtr, pNumber, pStudent.

8
#include<iostream> output
using namespace std;
int main() the address of num is : 0x6bfec8
{ the pointer variable pt holds : 0x6bfec8
the value of *pt is : 30
int num=30;
int * pt;
pt=&num;
cout<<" the address of num is : "<<&num<<endl;
cout<<" the pointer variable pt holds : "<<pt<<endl;
cout<<" the value of *pt is : "<<*pt;
}
Pointer initialization
 Pointers can be initialized to point to specific locations at the very moment they are
defined: int myvar;
int myvar; int * myptr;
int * myptr = &myvar; myptr = &myvar;

When pointers are initialized, what is initialized is the address they point to
(i.e., myptr), never the value being pointed (i.e., *myptr). Therefore, the code above
shall not be confused with:
int myvar;
int * myptr;
*myptr = &myvar; //not valid code.
Pointers can be initialized either to the address of a variable or to the value of
another pointer
int myvar;
int *foo = &myvar;
int *bar = foo;
Pointer has a Type
A pointer is associated with a type (of the value it points to), which is
specified during declaration.

A pointer can only hold an address of the declared type; it cannot hold
an address of a different type.
Pointers and Arrays

• Pointers are variables that hold addresses of other variables. Not only can a
pointer store the address of a single variable, it can also store the address of
cells of an array.

• The identifier of an array is equivalent to the address of its first element,


int numbers [20];
int * p;
The following assignment operation would be valid:
p = numbers;
• After that,

• p and numbers would be equivalent and would have the same properties.

• the only difference is that we could change the value of pointer p by another one,
whereas numbers will always point to the first of the 20 elements of type int with
which it was defined.

• Therefore, unlike p, which is an ordinary pointer, numbers is an array, and an


array can be considered a constant pointer.
#include<iostream>
using namespace std;
int main(){
int *ptr;
int scores[10]={10,20,30};
ptr=scores;
cout<<*ptr<<endl; //10
for(int i=0;i<4;i++) {
cout<<&scores[i]<<endl; } }
Note, the address of &scores[0] and scores is the same. It's because the variable
name scores points to the first element of the array. From the above example
&scores[0] is equivalent with scores and scores[0] is equivalent with *scores
&scores[1] is equivalent with scores+1 and scores[1] is equivalent with *(scores+1)
&scores[2] is equivalent with scores+2 and scores[2] is equivalent with *(scores+2)
• .
Arrays of Pointers
• Pointers may be arrayed like any other data type.

• The declaration for an int pointer array of size 10 is

• int *x[10];

• To assign the address of an integer variable called var to the third element of the
pointer array, write x[2] = &var;

• To find the value of var, write

• *x[2]
Pointer Arithmetic
• There are only two arithmetic operations that you may use on pointers:
addition and subtraction.

The valid operations that can be performed using pointers are

Addition of an integer to a pointer and increment operation.

 Subtraction of an integer from a pointer and decrement operation.


Suppose that we define three pointers in this compiler:

and that we know that they point to memory locations 1000, 2000 and 3000 respectively.
• To understand what occurs in pointer arithmetic, let p1 be an integer pointer with a
current value of 2000. Also, assume integers are 4 bytes long. After the expression p1+
+;

• p1 contains 2004, not 2001.

• The reason for this is that each time p1 is incremented, it will point to the next integer.

• The same is true of decrements. For example, assuming that p1 has the value 2000,

• the expression p1--; causes p1 to have the value 1996.


Pointers to pointers
• You can have a pointer point to another pointer that points to the target
value. This situation is called multiple indirection, or pointers to
pointers.
This, supposing the randomly chosen memory locations for each
variable of 7230, 8092 and 10502, could be represented as:

The value of each variable is written inside each cell; under the cells are their respective addresses in
memory.

 c has type char** and a value of 8092


 *c has type char* and a value of 7230
 **c has type char and a value of 'z'
Pointers and functions
• there is another way of passing arguments to a function where the
actual values of arguments are not passed.
void swap(int*, int*);
void swap(int* n1, int* n2)
swap(&a, &b);
Pointer to function
• To point to data, pointers are used. Like normal data pointers, we have function pointers that point
to functions. The address of a function is stored in a function pointer.

• The Basic syntax of function pointers

• void (*fun_ptr)(int);

• fun_ptr = &fun;

• We can think of function pointers like normal C++ functions. Where void is the function’s return
type. *fun_ptr is a pointer to a function that takes one int argument. It’s as if we are declaring a
function called *fun_ptr which takes int and returns void.
• Address of a function

• To get the address of a function, we must first state the function’s name. There is no need for us to call
the function.

• Consider the example below:

#include <iostream>

using namespace std;

int main()

cout << "The address of function main(): " <<&main<< endl;

return 0;

You might also like