Pointer
{C}
Programming
Programming for Problem Solving (PPS)
GTU # 3110003
USING
Prof. Nilesh Gambhava
�Computer Engineering Department,�Darshan Institute of Engineering & Technology, Rajkot
int n = 10;
int* p = &n; // Variable p of type pointer is pointing to the address of the variable n of type integer.
Declaring a pointer�
int *a; //pointer to int
char *c; //pointer to char
pointer variable stores the address of number variable, i.e., fff4. The value of number variable is 50. But the address of pointer variable p is aaa3.
#include<stdio.h>
int main()
{
int number=50;
int *p;
p=&number;//stores the address of number variable
printf("Address of p variable is %x \n",p);
printf("Value of p variable is %d \n",*p);
return 0;
}
O/P:
Address of p variable is fff4
Value of p variable is 50
�
Pointer to array
int arr[10];
int *p[10]=&arr; // Variable p of type pointer is pointing to the address of an integer array arr.
Pointer to structure
struct st {
int i;
float f;
}ref;
struct st *p = &ref;
Advantage of pointer�
1) Pointer reduces the code and improves the performance, it is used to retrieving strings, trees, etc. and used with arrays, structures, and functions.
2) We can return multiple values from a function using the pointer.
3) It makes you able to access any memory location in the computer's memory.
Usage of pointer�
There are many applications of pointers in c language.
1) Dynamic memory allocation
In c language, we can dynamically allocate memory using malloc() and calloc() functions where the pointer is used.
2) Arrays, Functions, and Structures
Pointers in c language are widely used in arrays, functions, and structures. It reduces the code and improves the performance.
Int* pc, c;
c = 5;
pc = &c;
Here, 5 is assigned to the c variable. And the address of c is assigned to the pc pointer.
To get the value of the thing pointed by the pointers, we use the * operator. For example:
int* pc, c;
c = 5;
pc = &c;
printf("%d", *pc); // Output: 5
What is Pointer?
Prof. Nilesh Gambhava
#3110003 (PPS) – Pointer
9
Pointers and Strings
Prof. Nilesh Gambhava
#3110003 (PPS) – Pointer
10
Pointers and Strings
Prof. Nilesh Gambhava
#3110003 (PPS) – Pointer
11
Pointers and Strings
Prof. Nilesh Gambhava
#3110003 (PPS) – Pointer
12
Relation between Array & Pointer
Prof. Nilesh Gambhava
#3110003 (PPS) – Pointer
13
Relation between Array & Pointer – Cont.
a[0]
a[1]
a[i]
a[9]
.
.
.
.
.
.
.
.
*(a+0)
*(a+1)
*(a+i)
*(a+9)
.
.
.
.
.
.
.
.
a:
a:
a+1:
a+i:
a+9:
2000
2002
2000 + i*2
2018
Prof. Nilesh Gambhava
#3110003 (PPS) – Pointer
14
Array of Pointer
datatype *name[size];
1
Syntax
int *ptr[5]; //declares an array of integer pointer of size 5
1
Example
Prof. Nilesh Gambhava
#3110003 (PPS) – Pointer
15
Array of Pointer – Cont.
for(i=0; i<5; i++)
{
ptr[i]=&mat[i][0];
}
1
2
3
4
Example
ptr[0]
ptr[2]
ptr[1]
ptr[3]
ptr[4]
0
1
2
ptr
Prof. Nilesh Gambhava
#3110003 (PPS) – Pointer
16
Pointer and Function
Prof. Nilesh Gambhava
#3110003 (PPS) – Pointer
17
Call by value�
We can observe that even when we change the content of x and y in the scope of the swap function,
these changes do not reflect on x and y variables defined in the scope of main. This is because we call swap() by value and it will get separate memory for x and y so the changes made in swap() will not reflect in main().
Prof. Nilesh Gambhava
#3110003 (PPS) – Pointer
18
�� Call by reference / address��
We can observe in function parameters instead of using int x, int y we used int *x, int *y and in function call instead of giving x, y, we give &x, &y this methodology is call by reference as we used pointers as function parameter which will get original parameters' address instead of their value. & operator is used to give address of the variables and * is used to access the memory location that pointer is pointing. As the function variable is pointing to the same memory location as the original parameters, the changes made in swap() reflect in main() which we can see in the above output.
Prof. Nilesh Gambhava
#3110003 (PPS) – Pointer
19
Pointer to Function
return-type (*ptr-function)(argument list);
1
Syntax
Prof. Nilesh Gambhava
#3110003 (PPS) – Pointer
20
#include<stdio.h>
int Sum(int,int);
int (*ptr)(int,int);
int main()
{
int a,b,rt;
printf("\nEnter 1st number : ");
scanf("%d",&a);
printf("\nEnter 2nd number : ");
scanf("%d",&b);
ptr = Sum;
rt = (*ptr)(a,b);
printf("\nThe sum is : %d",rt);
return 0;
}
int Sum(int x,int y)
{
return x + y;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Enter 1st number : 5
Enter 2nd number : 10
The sum is : 15
Program
Output
Write a program to sum of two numbers using pointer to function.
Prof. Nilesh Gambhava
#3110003 (PPS) – Pointer
21