You are on page 1of 13

SUBJECT CODE

TYPE THE SUBJECT NAME HERE

UNIT NO IV
BASICS OF C PROGRAMMING

4.5 Pointers-Pointer Operators-Pointer


Arthimettic-Array and pointers
I I

20ESCS101
Problem Solving and Programming in C
(Common to ALL Departments)
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )

POINTERS

❖ A pointer is a variable whose value is the address of another variable, i.e.,


direct address of the memory location.

❖ Like any variable or constant, you must declare a pointer before using it to
store any variable address.

❖ The general form of a pointer variable declaration is type *var-name.

❖ •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

for int i=5


20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )

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 )

Operators that are used with Pointers

perators & and * that are used with Pointers .

Address of(&) Operator

The & operator is also known as “Address of” Operator.

printf("Address of var is: %p", &num);

Value at Address”(*) Operator


The * Operator is also known as Value at address operator.
By using * operator we can access the value of a variable through a pointer
Example:

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 )

Pointers and Arrays:

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 )

Pointers and Arrays:

Example : int *ptr;


ptr = &arr[0];
ptr++;
printf(“The value of second array element is %d”, *ptr);
This printf will print the value 2 because the ptr variable that was originally pointing to base or first array element had been
incremented. Now it will point to the second array element
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )

Sorting of names in alphabetical order using pointers

#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

You might also like