You are on page 1of 7

Pointers in C

POINTER TO POINTER, POINTER TYPE & POINTER


ARITHMETIC

28‐Jun‐20 ARJ 1
What is Pointer to Pointer?
Consider the following
statements… x ptr1        ptr2
- int x = 10;
- int *ptr1; // pointer variable 10 3456  4000 

- ptr1 = &x;
3456 4000       5000
- int **ptr2; // pointer to pointer variable
- ptr2 = &ptr1;

28‐Jun‐20 ARJ 2
Pointer Type & Arithmetic
Data type - int * ptr;
◦ int – 4 bytes ptr = &x;
◦ char – 1 byte
◦ float – 4 bytes printf(“%d”, *ptr);
Consider the declaration –
int x = 1030;
Byte4 Byte3 Byte2 Byte1
00000000 00000000 00000100 00000110
6003 6002 6001 6000

28‐Jun‐20 ARJ 4
Pointer to a Constant
const int x = 3; // assigning to x is legal only at declaration

const int * p = &x;


OR
int const * p = &x; // same as const int * p

*p = 4; //assignment of read-only location '*p'

28‐Jun‐20 ARJ 6
Constant Pointer
Suppose we want to change *p, but not p itself

int * const p = &x;

*p=4. // legal statement

p =&y; // illegal

28‐Jun‐20 ARJ 7
Constant Pointer to a Constant
We want to prevent changing either where the pointer points,
or the value it points at…

const int * const p = &x;

C:\TurboC++\Disk\TurboC3\BIN\ptr16.cpp

28‐Jun‐20 ARJ 8
Possible combinations of constant and
pointer
The same principle applies to pointers to pointers (to
pointers to pointers…).

28‐Jun‐20 ARJ 9

You might also like