4.5 Pointers - Pointer Operators - Pointer Arithmetic - Arrays and Pointers - Array of Pointers - Example Program - Sorting of Names
4.5 Pointers - Pointer Operators - Pointer Arithmetic - Arrays and Pointers - Array of Pointers - Example Program - Sorting of Names
4.5 Pointers - Pointer Operators - Pointer Arithmetic - Arrays and Pointers - Array of Pointers - Example Program - Sorting of Names
UNIT NO IV
BASICS OF C PROGRAMMING
20ESCS101
Problem Solving and Programming in C
(Common to ALL Departments)
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )
POINTERS
❖ Like any variable or constant, you must declare a pointer before using it to
store any variable address.
❖ •type is the pointer's base type; it must be a valid C data type and var-name
is the name of the pointer variable.
❖ The asterisk * used to declare a pointer is the same asterisk used for
multiplication
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )
POINTERS
INITIALIZATION OF POINTER
During Declaration
float f=7.5;
float *p=&f; // pointer initialized during its declaration
After Declaration
float f=7.5;
float *p; // pointer declared
p=&f; // pointer initialized after declaration
EXAMPLE
#include <stdio.h>
int main()
{
int i = 5;
printf(" Address of i = %u",&i); // display address of i
printf("\n Value of i = %d",*(&i)); // display value of i using asterisk sign
printf("\n Value of i = %d",i); // display value of i
return 0;
}
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )
INITIALIZATION OF POINTER
Output:
Address of i = 2293324
Value of i = 5
Value of i = 5
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )
double a = 10;
double *p;
p = &a;
*p would give us the value of the variable a. The following statement would display 10 as output.
printf("%d", *p);
Similarly if we assign a value to *pointer like this:
*p = 200;
It would change the value of variable a. The statement above will change the value of a from 10 to 200
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )
Pointer arithmetic
int num1=2,num2=3,sum=0,mul=0;
int *ptr1,*ptr2;
Ptr1=&num1
Ptr2=&num2
num=*ptr1+ *ptr2
mul=sum * *ptr1;
printf(“%d”,num) o/p : 5
printf(“%d”,mul) 5*2=10
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )
We know that an array occupies consecutive memory locations. For example, consider an array shown below
int arr[] = {1,2,3,4,5}
It will be stored in the memory like
arr[0] arr[1] arr[2] arr[3]
arr[5]
1000 1002 1004
1006 1008
Example :
int *ptr;
ptr = &arr[0];
In this example a pointer variable is created , namely ptr which points to the first element of the array.
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <alloc.h>
void main()
{
char *a[10],dum[10],s;
int i,k,j,n;
clrscr();
printf("enter the no of std....");
scanf("%d",&n);
printf("enter the name of students ");
for(k=0;k<n;k++)
scanf("%s",a[k]);
} }
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )
for(i=1;i<n;i++)
{
for(j=1;j<n-i;j++)
{if(strcmp(a[j-1],a[j])>0)
{strcpy(*dum,*a[j-1]);
strcpy(*a[j-1],*a[j]);
strcpy(*a[j],*dum);
}
}
} }
for(i=0;i<n;i++)
printf("%s",a[i]);
getch();
}
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )
Output:-
Enter 5 strings:
tree
bowl
hat
mice
toon
The strings are:
tree
bowl
hat
mice
toon
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )
Video link
https://www.youtube.com/watch?
v=j2OkATAcQAk