Pointers
What Are Pointers?
• A pointer is a variable whose value is the address of
another variable, i.e., direct address of the memory
location.
type *var-name;
• type is the pointer's base type; it must be a valid C data
type.
• var-name is the name of the pointer variable.
• The asterisk * you used to declare a pointer is the same
asterisk that you use for multiplication. In this statement
the asterisk is being used to designate a variable as a
pointer.
What Are Pointers?
• The actual data type of the value of all pointers, whether
integer, float, character, or otherwise, is the same, a long
hexadecimal number that represents a memory address.
• The only difference between pointers of different data
types is the data type of the variable or constant that the
pointer points to.
Consider the declaration,
int i = 3 ;
This declaration tells the C compiler to:
(a) Reserve space in memory to hold the integer value.
(b) Associate the name i with this memory location.
(c) Store the value 3 at this location
# include <stdio.h>
int main( )
{
int i = 3 ;
printf ( "Address of i = %u\n", &i ) ;
printf ( "Value of i = %d\n", i ) ;
return 0 ;
}
• The other pointer operator available in C is ‘*’, called
‘value at address’ operator.
• It gives the value stored at a particular address. The
‘value at address’ operator is also called ‘indirection’
operator.
• The expression &i gives the address of the variable i. This
address can be collected in a variable, by saying
j = &i ;
Since j is a variable, the compiler must provide it space in
the memory.
# include <stdio.h>
int main( )
{
int i = 3 ;
int *j ;
j = &i ;
printf ( "Address of i = %u\n", &i ) ;
printf ( "Address of i = %u\n", j ) ;
printf ( "Address of j = %u\n", &j ) ;
printf ( "Value of j = %u\n", j ) ;
printf ( "Value of i = %d\n", i ) ;
printf ( "Value of i = %d\n", *( &i ) ) ;
printf ( "Value of i = %d\n", *j ) ;
return 0 ;
}
int *alpha ;
char *ch ;
float *s ;
The declaration float *s does not mean that s is going to
contain a floating-point value. s is going to contain the
address of a floating-point value.
A pointer variable can hold address of another pointer
variable.
Function Calls
The two types of function calls
call by value
call by reference.
Arguments can generally be passed to functions in one of
the two ways:
(a) sending the values of the arguments
(b) sending the addresses of the arguments
•# include <stdio.h> # include <stdio.h>
void swapv ( int x, int y ) ; void swapr ( int *, int * ) ;
int main( ) int main( )
{ {
int a = 10, b = 20 ; int a = 10, b = 20 ;
swapv ( a, b ) ; swapr ( &a, &b ) ;
printf ( "a = %d b = %d\n", a, b ) ; printf ( "a = %d b = %d\n", a, b ) ;
return 0 ; return 0 ;
} }
void swapv ( int x, int y ) void swapr ( int *x, int *y )
{ {
int t ; int t ;
t=x; t = *x ;
x=y; *x = *y ;
y=t; *y = t ;
printf ( "x = %d y = %d\n", x, y ) }
;
}
Using a call by reference intelligently, we can make a
function return more than one value at a time, which is not
possible ordinarily.
# include <stdio.h> void areaperi (int r, float *a, float *p)
void areaperi ( int, float *, float * ) ;
int main( ) {
{ *a = 3.14 * r * r ;
int radius ; *p = 2 * 3.14 * r ;
float area, perimeter ; }
printf ( "Enter radius of a circle " ) ;
scanf ( "%d", &radius ) ;
areaperi ( radius, &area, &perimeter ) ;
printf ( "Area = %f\n", area ) ;
printf ( "Perimeter = %f\n", perimeter ) ;
return 0 ;
}
How to use Pointers?
• We define a pointer variable.
• Assign the address of a variable to a pointer
• Finally access the value at the address available in the
pointer variable.
NULL Pointers in C
• It is always a good practice to assign a NULL value to a
pointer variable in case you do not have exact address to
be assigned. This is done at the time of variable
declaration.
• A pointer that is assigned NULL is called a null pointer.
• On most of the operating systems, programs are not
permitted to access memory at address 0 because that
memory is reserved by the operating system.
• However, the memory address 0 has special significance;
it signals that the pointer is not intended to point to an
accessible memory location. But by convention, if a
pointer contains the null (zero) value, it is assumed to
point to nothing.
Pointer arithmetic
• We can perform arithmetic operations on a pointer just
as you can a numeric value. There are four arithmetic
operators that can be used on pointers: ++, --, +, and - .
• let us consider that ptr is an integer pointer which points
to the address 1000. ptr++ will point to the location 1004
because each time ptr is incremented, it will point to the
next integer location which is 4 bytes next to the current
location.
• We prefer using a pointer in our program instead of an
array because the variable pointer can be incremented,
unlike the array name which cannot be incremented
because it is a constant pointer.
• You have to complete the function
void update(int *a,int *b),
which reads two integers as argument, and sets with the
sum of them, and with the absolute difference of them.
• Input Format
Input will contain two integers a and b separated by a
newline.
• Output Format
You have to print the updated value of a and b on two
different lines.
Pointer to Pointer
• A pointer to a pointer is a form of multiple indirection, or
a chain of pointers. Normally, a pointer contains the
address of a variable.
• When we define a pointer to a pointer, the first pointer
contains the address of the second pointer, which points
to the location that contains the actual value as shown
below.
int **var
Practise
• Write a program in C to add two numbers using pointers.
• Write a program in C to find the maximum number
between two numbers using a pointer.
• Write a program in C to print all permutations of a given
string using pointers.