COMPUTER
PROGRAMMING
LECTURE # POINTERS
1
ADDRESS OF A VARIABLE
Every variable is allocated a memory space large enough
to hold a value of the variable’s data type.
char -> 1 byte
short -> 2 bytes
int -> 4 ..
long -> 4 ..
float -> 4 ..
double -> 8 ..
Each byte of memory has a unique address.
A variable’s address is the address of the first
byte allocated to that variable.
ADDRESS OF A VARIABLE
Suppose the following variables are defined in a
program:
char letter;
short number;
float amount;
Possible arrangement in the memory
Bytes of memory
9.1
Getting the Address of a Variable
GETTING THE ADDRESS OF A VARIABLE
Use address operator & to get address of a variable:
int num = -99;
cout << # // prints address
// in hexadecimal
&num: gives the address of variable num
& is called Address-of-operator
Do not confuse the address operator with the & symbol
used when defining a reference variable.
GETTING THE ADDRESS OF A VARIABLE
9.2
Pointer Variables
POINTER VARIABLES
Pointer variable : Often just called a pointer, it's a variable that
holds an address i.e.
Don’t hold any value rather hold the address of another variable
Because a pointer variable holds the address of another piece of
data, it "points" to the data
SOMETHING LIKE POINTERS: ARRAYS
We have already worked with something similar
to pointers, when we learned to pass arrays as
arguments to functions.
Forexample, suppose we use this statement to
pass the array numbers to the showValues
function:
showValues(numbers, SIZE);
SOMETHING LIKE POINTERS : ARRAYS
The values parameter, in the showValues
function, points to the numbers array.
C++ automatically stores
the address of numbers in
the values parameter.
SOMETHING LIKE POINTERS:
REFERENCE VARIABLES
We have also worked with reference variables when we
learned to use reference variables. Suppose we have this
function:
void getOrder(int &donuts)
{
cout << "How many doughnuts do you want? ";
cin >> donuts;
}
And we call it with this code:
int jellyDonuts;
getOrder(jellyDonuts);
SOMETHING LIKE POINTERS:
REFERENCE VARIABLES
The donuts parameter, in the getOrder function,
points to the jellyDonuts variable.
C++ automatically stores
the address of
jellyDonuts in the
donuts parameter.
SOMETHING LIKE POINTERS:
REFERENCE VARIABLES
Reference variable is similar to a pointer.
When you are storing a value in the donuts variable,
you don’t have to specify that the value should actually
be stored in the jellyDonuts variable.
C++ handles all of that automatically.
POINTER VARIABLES
Pointer
variables are yet another way of using a
memory address to work with a piece of data.
Pointersare more "low-level" than arrays and
reference variables.
Thismeans you are responsible for finding the
address you want to store in the pointer and
correctly using it.
POINTER VARIABLES
Definition:
int *intptr;
Read as:
“intptr can hold the address of an int”
Spacing in definition does not matter:
int * intptr; // same as above
int* intptr; // same as above
POINTER VARIABLES
Assigning an address to a pointer variable:
int *intptr;
intptr = #
Memory layout:
num intptr
25 0x4a00
address of num: 0x4a00
9.3
The Relationship Between Arrays and Pointers
THE RELATIONSHIP BETWEEN ARRAYS
AND POINTERS
Array name is starting address of array
int x[3] = {4, 7, 11};
x[0] x[1] x[2]
4 7 11
1000 1002 1004
starting address of x: 1000
cout << x; // displays
// 1000
cout << x[0]; // displays 4
THE RELATIONSHIP BETWEEN ARRAYS
AND POINTERS
Array name can be used as a pointer constant:
int X[] = {4, 7, 11};
cout << *vals; // displays 4
Pointer can be used as an array name:
int *valptr = vals;
cout << valptr[1]; // displays 7
9-21
POINTERS IN EXPRESSIONS
Given:
int vals[]={4,7,11}, *valptr;
valptr = vals;
What is valptr + 1?
It means (address in valptr) + (1 * size of an int)
cout << *(valptr+1 * size of an int);
//displays 7
cout << *(valptr+2 * size of an int);
//displays 11
Must use ( ) as shown in the expressions
FROM PROGRAM 9-7
COMPARING POINTERS
Relational operators (<, >=, etc.) can be used to compare
addresses in pointers
Comparing addresses in pointers is not the same as
comparing contents pointed at by pointers:
if (ptr1 == ptr2) // compares
//
addresses
if (*ptr1 == *ptr2) // compares
// contents
9-24
9.7
Pointers as Function Parameters
POINTERS AS FUNCTION PARAMETERS
A pointer can be a parameter
Works like reference variable to allow change to
argument from within function
Requires:
void getNum(int *ptr); // ptr is pointer to an int
……
cin >> *ptr; //using the pointer
Call the function by providing address as argument
getNum(&num); // pass address of num to getNum
EXAMPLE
int num1 = 2, num2 = -3;
swap(&num1, &num2);
void swap(int *x, int *y)
{ int temp;
temp = *x;
*x = *y;
*y = temp;
}
(Program Continues)