Professional Documents
Culture Documents
IS0403
Pointers
Introduction
Pointer is a derived data type, built from one of the
fundamental data types available in C.
Pointers are variables that contain memory addresses as their
values.
A variable name directly references a value.
A pointer indirectly references a value. Referencing a value
through a pointer is called indirection.
A pointer variable must be declared before it can be used.
Pointer is used to access and manipulate data stored in
memory
Benefits of Pointers
More efficient in handling arrays and data tables
Used to return multiple values from a function via function argument
Permit references to functions and there by facilitating passing of
functions as arguments to other functions.
Support dynamic memory management
Provide an efficient tool for manipulating dynamic data structures
such as structures, linked lists, queues, stacks and trees.
Reduce length and complexity of programs.
Increase execution speed and thus reduce execution time.
Understanding Pointers
Computer memory is a sequential collection of storage
cells.
Each cell known as byte, has a number called address
associated with it.
Addresses are numbered consecutively, starting from zero.
Last address depends on the memory size.
When a variable is declared ,the system allocates
somewhere in the memory an appropriate location to hold
the value of the variable.
Cont . . .
Example:
int number = 229;
Statement instructs the system to find a location for the integer variable
number, and put the value 229. (assume 5000 as address)
During execution, the system always associates the name number with the
address 5000.
May access the value 229 using name or address.
As memory address are simply numbers, can be assigned to some variables,
that can be stored in memory.
Variables that hold memory addresses are called pointer variables.
Cont . . .
Pointer variable is nothing but a variable that contains
address, which is a location of another variable in memory.
Since a pointer is a variable, its value is also stored in the
memory in another location
If we assign the address of number to a variable p.
The link between p and number is
z = (5 * (- (*p2)))/ (*p1);
*p2 = *p2 + 10;
Cont . . .
z = 5 * - *p2 /*p1; is valid or invalid???
/* means beginning of comment, so statement is invalid.
Expressions like p1 + 4, p2 – 2, p1 – p2 are all allowed and
valid.
If p1 and p2 are both pointers to the same array, then p2 –
p1 gives the number of elements between p1 and p2.
Shorthand operators can be used with pointers.
• Example: p1++; -p2; sum += *p2;
Cont . . .
Pointers can also be compared using relational operators.
• Example: p1 > p2; p1 == p2; p1 != p2;
We may not use pointers in division or multiplication
• Example: p1 / p2 or p1 * p2 or p1 / 3 are not allowed
Two pointers cannot be added.
• Example: p1 + p2 is illegal
Pointer Increments and Scale Factor
Pointers can be incremented like
p1 = p2 + 2; p1 = p1 + 1;
However an expression like p1++; will cause the pointer
p1 to point to the next value of its type.
Example:
• If p1 is an integer pointer with an initial value of 2800, after increment
its value will be 2802 and not 2801.
• When a pointer is incremented, its value is incremented by the length of
the data type that it points to, called as scale factor.
Number of bytes used to store various data types depends on the
system and can be found by using sizeof operator
Rules of Pointer Operations
Pointer variable can be
• Assigned the address of another variable
• Assigned the values of another pointer variable
• Initialized with NULL or zero value
• Pre-fixed or post fixed with increment or decrement operator
• Integer value may be added or subtracted from a pointer variable
• Pointer variable cannot be multiplied by a constant
• Two pointer variables cannot be added
• Value cannot be assigned to an arbitrary address(&x=10; is illegal)
• When two pointer point to the same array, one pointer variable can be
subtracted from another.
• When two pointers point to the objects of the same data types, they can be
compared using relational operator.
Pointers and arrays
When an array is declared, the compiler allocates a base address and
sufficient amount of storage to contain all the elements of the array
in contiguous memory locations.
Base address is the location of the first element of the array.
Compiler also defines the array name as a constant pointer to the first
element.
Example:
int x[5] = { 1, 2, 3, 4, 5 };
Elements x[0] x[1] x[2]x[3] x[4]
Value
Address 1000 1002 1004 1006 1008
1 2 3 4 5
Base Address
Cont . . .
x is defined as a constant pointer pointing to the first element x[0]
Therefore the value of x is 1000, the location where x[0] is stored.
Ex: x = &x[0] = 1000
If p is declared as an integer pointer, then pointer p is pointed to the
array x by
p = x; p = &x[0];
Every value of x can be accessed by using p++, to move from one
element to another.
Ex: address of x[3] = base address + (3 * scale factor of int)
= 1000 + (3 * 2) = 1006
Cont . . .
When handling arrays, instead of using array indexing,
pointers can be used to access array elements.
* (p + 3) gives the value of x[3].
The pointer accessing method is much faster than array
indexing.
Cont . . .
Pointers can be used to manipulate two dimensional arrays
as well.
In one dimensional array x, the expression *(x +i) or *(p+i)
represents the element x[i]
Similarly an element in a two dimensional array can be
represented by the pointer expression
* ( * ( a + i ) + j ) or * ( * ( p + i ) + j )
Cont . . .
Columns
0 1 2 3 4 5
0 p
1 p+1
Rows 2
3
4 4,0 4,3 p+4
5
6 p+6
* (p + 4) * (p + 4) + 3
p pointer to first row p + i pointer to ith row
*(p + i) pointer to first element in ith row
*(p + i) +j pointer to jth element in ith row
* ( *(p + i) + j) value stored in the cell (i, j)
Cont . . .
The base address of the array a is &a[0][0] and starting at
this address, the compiler allocates contiguous space for all
the elements row-wise.
First element of the second row is placed immediately after
the last element of the row and so on.
If array a is declared as follows:
int a[3][4] = { {15,27,11,35}
{22,19,31,17}
{31,23,14,36}};
Cont . . .
Then elements of a is stored as:
row 0 row 1 row 2
15 27 11 35 22 19 31 17 31 23 14 36
Function Task
Allocates request size of bytes and returns a pointer to
malloc the first byte of the allocated space