Pointers: Basics
Pointers: Basics
What is a pointer?
First of all, it is a variable, just like other
variables you studied
So it has type, storage etc.
Difference: it can only store the address
(rather than the value) of a data item
Type of a pointer variable – pointer to the type
of the data whose address it will store
Example: int pointer, float pointer,…
Can be pointer to any user-defined types also like
structure types
2
They have a number of useful applications
Enables us to access a variable that is defined
outside the function
Can be used to pass information back and forth
between a function and its reference point
More efficient in handling data tables
Reduces the length and complexity of a program
Sometimes also increases the execution speed
Basic Concept
As seen before, in memory, every stored data item
occupies one or more contiguous memory cells
The number of memory cells required to store a
data item depends on its type (char, int, double,
etc.).
Whenever we declare a variable, the system
allocates memory location(s) to hold the value of the
variable.
Since every byte in memory has a unique
address, this location will also have its own
(unique) address.
4
Contd.
Consider the statement
int xyz = 50;
This statement instructs the compiler to
allocate a location for the integer variable xyz,
and put the value 50 in that location
Suppose that the address location chosen is
1380 xyz variable
50 value
1380 address
5
Contd.
During execution of the program, the system always
associates the name xyz with the address 1380
The value 50 can be accessed by using either the
name xyz or the address 1380
Since memory addresses are simply numbers, they
can be assigned to some variables which can be
stored in memory
Such variables that hold memory addresses are
called pointers
Since a pointer is a variable, its value is also
stored in some memory location
6
Contd.
Suppose we assign the address of xyz to a
variable p
p is said to point to the variable xyz
7
Address vs. Value
Each memory cell has an address
associated with it
8
Address vs. Value
Each memory cell has an address
associated with it
Each cell also stores some value
9
Address vs. Value
Each memory cell has an address
associated with it
Each cell also stores some value
Don’t confuse the address referring to a
memory location with the value stored in that
location
10
Values vs Locations
1024: 32 value
x
address
name
11
Pointers in C
A pointer is just a C variable whose value can
contain the address of another variable
Needs to be declared before use just like any other
variable
General form:
data_type *pointer_name;
13
Pointers can be defined for any type, including
user defined types
Example
struct name {
char first[20];
char last[20];
};
struct name *p;
int arr[20];
:
&arr;
Pointing at array name
&(a+b)
Pointing at expression
18
Accessing a Variable Through
its Pointer
Once a pointer has been assigned the address
of a variable, the value of the variable can be
accessed using the indirection operator (*).
int a, b;
int *p; Equivalent to b = a;
p = &a;
b = *p;
19
Example
#include <stdio.h>
int main()
{ Equivalent
int a, b;
int c = 5;
int *p;
a = 4 * (c + 5) ;
p = &c;
b = 4 * (*p + 5) ;
printf (“a=%d b=%d \n”, a, b);
return 0; a=40 b=40
}
20
Example
int main()
{
int x, y;
int *ptr;
x = 10 ;
ptr = &x ;
y = *ptr ;
printf (“%d is stored in location %u \n”, x, &x);
printf (“%d is stored in location %u \n”, *&x, &x);
printf (“%d is stored in location %u \n”, *ptr, ptr);
printf (“%d is stored in location %u \n”, y, &*ptr);
printf (“%u is stored in location %u \n”, ptr, &ptr);
printf (“%d is stored in location %u \n”, y, &y);
*ptr = 25;
printf (“\nNow x = %d \n”, x);
return 0;
} 21
Suppose that
Address of x: 3221224908
Address of y: 3221224904
Address of ptr: 3221224900
Then output is
10 is stored in location 3221224908
10 is stored in location 3221224908
10 is stored in location 3221224908
10 is stored in location 3221224908
3221224908 is stored in location 3221224900
10 is stored in location 3221224904
Now x = 25
22
Example
int x; pointer to int
1024: 32
int ∗ xp ;
x
xp = &x ; 1024
xp
address of x
∗xp = 0; /* Assign 0 to x */
∗xp = ∗xp + 1; /* Add 1 to x */
23
Value of the pointer
Declaring a pointer just allocates space to hold the
pointer – it does not allocate something to be
pointed to!
Localvariables in C are not initialized, they may contain
anything
1500
2300
25
0
26
0
p: 1500 2300
27
0
28
0
Memory and Pointers:
int v, *p;
p = &v;
v: 1500
25
v = 17;
*p = *p + 4;
p: 1500 2300
v = *p + 4
29
More Examples of Using Pointers
in Expressions
If p1 and p2 are two pointers, the following
statements are valid:
int *count;
count = 1268;
31
Whenever you use *p to access the value of the location
pointed to by a pointer variable p, always check that p
has been assigned a valid value before by an assignment
statement ( p = …..)
Very common mistake while writing programs with pointers
34
Scale Factor
We have seen that an integer value can be
added to or subtracted from a pointer variable
int *p1, *p2;
int i, j;
:
p1 = p1 + 1;
p2 = p1 + j;
p2++;
p2 = p2 – (i + j);