AOP Unit 2 Chapter 3.Pptx
AOP Unit 2 Chapter 3.Pptx
PROGRAMMING
Unit 2-Chapter 3-Pointers
Dr. Salini Suresh
Associate Professor ,DSCASC
Pointers
Whenever a variable is declared like int, char, float etc. The
declaration tells the compiler to :
1) Reserve space in memory depending on the datatype.
2) Associate the variable name with that memory location.
3) To Store the respective value at that location.
Ex. : int i = 3;
The Memory Map will be :
Pointers
The normal variable when declared always stores the value
of the variable and not the address.
2 ) float *ptr ;
Declares a pointer by name ptr which can store the address of only the float
variable.
POINTER OPERATOR :
It is the ‘*’ which is used at the time of declaration of the variable to
identify the variable as a pointer and not a normal variable.
ASSIGNING THE VALUE TO THE POINTERS
Once the pointer is declared it must be assigned with the address of the
variable which is pointed to.
It can only be assigned with the address of another variable and not by the
value directly.
# include <stdio.h>
void main ( )
{
int n, *ptr;
ptr = &n;
n = 10;
printf (“ The value of n = %d”, n);
printf (“The address of n = %u”, ptr);
}
Accessing a value through a POINTER
INDIRECT OPERATOR :
It is the operator used along with the pointer to access the value
stored at the address of variable referred by the pointer.
Ex.: int i = 5, x;
int *p;
p = &i;
x = *p;
P2 P1 variable
Here pointer variable p2 contains the address of the pointer variable p1,
which points to the location that contains the desired value.
This is known as multiple indirections.
It performs the operation not on the address but on the value at the address
which is pointed to.
Eg.: If ptr & ptr1 are two pointer variables storing the address of a & b
respectively with the value 10 and 5 then the following arithmetic
expression will result in
*ptr + *ptr1
= 10 + 5 = 15.
*ptr + 10
= 10 + 10 = 20.
*ptr1 ++
= 5++ = 6.
ptr1++ will increment to next location.
Pointer increments And scale factor
Pointers can be incremented like
p1= p2 + 2;
p1 = p1 + 1; or p1++;
If p1 is an integer pointer pointer with an initial value 2800, then after the
operation p1=p1+1, the value of p1 will be 2802 and not 2801.
P = &x[0] (=1000)
P+1= &x[1] (=1002)
P+2= &x[2] (=1004)
P+3= &x[3] (=1006)
P+4= &x[4] (=1008)
The address of an element is calculated using its index and the scale
factor of the data type.
main()
{
int *p, i;
int x[5] = {5, 9, 6, 3, 7};
i=0;
p=x;
printf (“ Element Value ”);
while(i<5)
{
printf(“ x[%d] %d ”, i, *p);
i++;
++p;
}
}
Accessing one dimensional array elements using the pointer
0 1 2 3 4 5
0
1
2
3
4
5
6
Syntax:
int *var_name[array_size];
We can make separate pointer variables which can point to the different values or
we can make one integer array of pointers that can point to all the values
Array of Pointers
// C program to demonstrate example of array of pointers.
#include <stdio.h>
const int SIZE = 3;
void main()
{
// creating an array
int arr[] = { 1, 2, 3 };
// we can make an integer pointer array to storing the address of array elements
int i, *ptr[SIZE];
In this case the values of each actual arguments are copied onto the
corresponding formal arguments of the called function.
Call by value:
C program to illustrate call by value
#include<stdio.h> • Since the values are passed the
void num(int,int); changes made to the formal
void main() arguments within ,
{ • the called function will not have
int a=10,b=20; any effect on the values of actual
printf(“a= %d and b=%d before the function call “, a,b); arguments in the calling function.
num(a, b);
printf(“a= %d and b=%d after the function call using call by value
“);
}
Output :
a=10 and b=20 before the function call
a=10 and b=20 after the function call using call by value
Pointers And Functions
2) Call by Reference:
In this method the address of the actual parameters & not the
values are passed into the formal argument of the called function.
This tells the compiler that fptr is a pointer to a function, which returns
type value.
Examples:
Two dimensional array:
int two_d[10][20];
Example:int x[2][1];
The above example represents the element present in third row and
second column.
The command line arguments are handled using main() function arguments.
#include <stdio.h>
When the above code is compiled and
int main( int argc, char *argv[] ) { executed with single argument, it produces
the following result.
if( argc == 2 ) { The argument supplied
printf("The argument supplied is %s\n",
argv[1]); When the above code is compiled and
} executed with a two arguments, it produces
else if( argc > 2 ) { the following result.
printf("Too many arguments supplied.\n"); Too many arguments supplied.
}
else { When the above code is compiled and
printf("One argument expected.\n"); executed without passing any argument, it
} produces the following result.
} One argument expected