Unit-7 PPS
Unit-7 PPS
Unit-7 PPS
1
Declaration & Initialization of Pointer
Syntax Output
1 datatype *ptr_variablename; 10 10 5000
Example
1 void main()
2 { Variable Value Address
3 int a=10, *p; // assign memory address of a
4 to pointer variable p a 10 5000
5 p = &a;
6 printf("%d %d %d", a, *p, p); p 5048
7 }
5000
3
Pointer to Pointer – Double Pointer
Pointer holds the address of another variable of same type.
When a pointer holds the address of another pointer then such type of pointer is
known as pointer-to-pointer or double pointer.
The first pointer contains the address of the second pointer, which points to the
location that contains the actual value.
Syntax
Pointer Pointer Variable
1 datatype **ptr_variablename;
address address value
Example
1 int **ptr;
4
Write a program to print variable, address of pointer variable and pointer to pointer variable.
Output
Value of var = 3000
Value available at *ptr = 3000
Value available at **pptr = 3000
5
Relation between Array & Pointer
When we declare an array, compiler allocates continuous blocks of memory so that
all the elements of an array can be stored in that memory.
The address of first allocated byte or the address of first element is assigned to an
array name.
Thus array name works as pointer variable.
The address of first element is also known as base address.
6
Relation between Array & Pointer – Cont.
Example: int a[10], *p;
a[0] is same as *(a+0), a[2] is same as *(a+2) and a[i] is same as *(a+i)
7
Array of Pointer
As we have an array of char, int, float etc, same way we can have an array of pointer.
Individual elements of an array will store the address values.
So, an array is a collection of values of similar type. It can also be a collection of
references of similar type known by single name.
Syntax
1 datatype *name[size];
Example
1 int *ptr[5]; //declares an array of integer pointer of size 5
8
Array of Pointer – Cont.
An array of pointers ptr can be used to point to different rows of matrix as follow:
Example
1 for(i=0; i<5; i++)
2 {
3 ptr[i]=&mat[i][0];
4 }
ptr 0 1 2
ptr[0]
ptr[1]
ptr[2]
ptr[3]
ptr[4]
Output
Enter value of num1 and num2: 5
10
Before Swapping: num1 is: 5, num2 is: 10
After Swapping: num1 is: 10, num2 is: 5
10
Pointer and Function
Like normal variable, pointer variable can be passed as function argument and
function can return pointer as well.
There are two approaches to passing argument to a function:
Call by value
Call by reference / address
11
Call by Value
In this approach, the values are passed as function argument to the definition of
function.
Program Output
1 #include<stdio.h> Values before calling 10, 20
2 void fun(int,int); Values after calling 10, 20
3 int main()
4 {
5 int A=10,B=20;
6 printf("\nValues before calling %d, %d",A,B);
7 fun(A,B); Address 48252 24688
8 printf("\nValues after calling %d, %d",A,B);
9 return 0; Value 10 20 10 11 20 22
10 }
11 void fun(int X,int Y) Variable A B X Y
12 {
13 X=11;
14 Y=22;
15 }
12
Call by Reference / Address
In this approach, the references / addresses are passed as function argument to the
definition of function.
Program
Output
1 #include<stdio.h>
Values before calling 10, 20
2 void fun(int*,int*); Values after calling 11, 22
3 int main()
4 {
5 int A=10,B=20;
6 printf("\nValues before calling %d, %d",A,B);
7 fun(&A,&B); Address 24688
48252
8 printf("\nValues after calling %d, %d",A,B);
9 return 0; Value 10 11 20 22 48252 24688
10 }
11 void fun(int *X,int *Y) Variable A B *X *Y
12 {
13 *X=11;
14 *Y=22;
15 }
13
Pointer to Function
Every function has reference or address, and if we know the reference or address of
function, we can access the function using its reference or address.
This is the way of accessing function using pointer.
Syntax
1 return-type (*ptr-function)(argument list);
14
Write a program to sum of two numbers using pointer to function.
15
Practice Programs
1. Write a C program to print the address of variable using pointer.
2. Write a C a program to swap two elements using pointer.
3. Write a C a program to print value and address of a variable
4. Write a C a program to calculate sum of two numbers using pointer
5. Write a C a program to swap value of two numbers using pointer
6. Write a C a program to calculate sum of elements of an array using pointer
7. Write a C a program to swap value of two variables using function
8. Write a C a program to print the address of character and the character of string using pointer
9. Write a C a program for sorting using pointer
16
Thank you