Pointer
Pointer
Pointer
Pointers
Pointer Variables
The first step in understanding pointers is
visualizing what they represent at the
machine level.
In most modern computers, main memory is
divided into bytes, with each byte capable of
storing eight bits of information:
3
Pointer Variables
Each variable in a program occupies one or
more bytes of memory.
The address of the first byte is said to be the
address of the variable.
In the following figure, the address of the
variable i is 2000:
4
Pointer Variables
Addresses can be stored in special pointer
variables.
When we store the address of a variable i in
the pointer variable p, we say that p “points
to” i.
A graphical representation:
5
Declaring Pointer Variables
Pointer variables can appear in declarations along with
other variables:
int i, j, a[10], b[20], *p, *q;
C requires that every pointer variable point only to
objects of a particular type (the referenced type):
int *p; /* points only to integers */
double *q; /* points only to doubles */
char *r; /* points only to characters */
There are no restrictions on what the referenced type
may be.
6
Pointer Variable Declaration and
Initialization
Pointer declaration
Multiple pointers require using a * before each
variable definition
int *myPtr1, *myPtr2;
Can define pointers to any data type
It’s crucial to initialize p before we use it.
Initialize pointers to 0, NULL, or an address
0 or NULL – points to nothing (NULL preferred)
The Address and Indirection
Operators
C provides a pair of operators designed
specifically for use with pointers.
&
To find the address of a variable, we use the & (address)
operator.
*
To gain access to the object that a pointer points to, we
use the * (indirection) operator.
8
Pointer Operators
& (address operator)
Returns address of operand
int y = 5;
int *yPtr;
yPtr = &y; /* yPtr gets address of y */
yPtr “points to” y
y yptr y
5 500000 600000 600000 5
yPtr
Address of y
is value of
yptr
The Address Operator
It’s also possible to initialize a pointer variable
at the time it’s declared:
int i;
int *p = &i;
The Indirection Operator
p = &i;
i = 1;
*q = 2;
*q = *p;
Comparing Pointers
18
6.4 Pointers in Function
References (!IMPORTANT!)
In C, function references are call-by-value except when
an array name is used as an argument.
An array name is the address of the first element
Values in an array can be modified by statements within a
function
To modify a function argument, a pointer to the argument
must be passed
scanf(“%f”, &X); This statement specifies that the value
read is to be stored at the address of X
The actual parameter that corresponds to a pointer
argument must be an address or pointer.
19
Call by reference
to
MOSTLY USED WITH ARRAYS (see next section)
Example: p++;
if p is defined as int *p, p will be incremented by 4 (system dependent)
if p is defined as double *p, p will be incremented by 8(system
dependent
when applied to pointers, ++ means increment pointer to point to next
value in memory
21
6.2 Pointers and Arrays
The name of an array is the address of the first elements (i.e. a pointer
to the first element)
The array name is a constant that always points to the first element of
Example
int num[4] = {1,2,3,4}, *p, q[];
p = num;
q = p; // or q = num;
/* above assignment is the same as p = &num[0]; */
printf(“%i”, *p); // print num[0]
p++;
printf(“%i”, *p); // print num[1]
printf(“%i”, *q); // print num[0]
printf(“%i”, *(p+2)); // print num[2]
22
Two Dimensional Arrays
A two-dimensional array is stored in sequential memory
locations, in row order.
int s[2][3] = {{2,4,6}, {1,5,3}};
25