Chapter 7
Chapter 7
CHAPTER 7
Pointers
Example
#include<stdio.h>
void main()
{
int x=10,y=20,z=30;
int *arr[3]; // Declaring array of three pointer
CKPCET, SURAT 1
PPS (3110003)
printf("\nValues : ");
for(int a=0;a<3;a++)
printf("%d, ",*arr[a]);
}
Output :
Values : 10, 20, 30,
In the above example, we have declared three variable x, y, z and assigning the addresses of these
variables into an array of pointer(*arr[]).
Q.2 What is Pointer in C? Explain its advantages. Also write a program to swap two
elements using pointer. .(CPU_WIN_2017) (CPU_WIN_2015) (CPU_WIN_2014)
(CPU_WIN_2014)(CPU_SUM_2017)
A pointer is a variable that contains an address which is a location of another variable in memory.
Advantages :
• Pointers reduce the length and complexity of a program.
• They increase execution speed.
• A pointer enables us to access a variable that is defined outside the function.
• Pointers are more efficient in handling the data tables.
• The use of a pointer array of character strings results in saving of data storage space in
memory.
#include <stdio.h>
int main()
{
int x, y, t;
printf("Enter two integers\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\nFirst integer = %d\nSecond integer = %d\n", x, y);
t = x;
x = y;
y = t;
printf("After Swapping\nFirst integer = %d\nSecond integer = %d\n", x, y);
return 0;
CKPCET, SURAT 2
PPS (3110003)
}
OUTPUT
Enter two integers
23
45
Before Swapping
First integer = 23
Second integer = 45
After Swapping
First integer = 45
Second integer = 23
Q.3 What is a pointer? How is a pointer initialized? (CPU_WIN_2016)
(CPU_WIN_2013)(CPU_SUM_2019)
A pointer is a variable whose value is the address of another variable, i.e., direct address of the
memory location.
General syntax of pointer declaration is,
datatype *pointer_name;
Pointer Initialization is the process of assigning address of a variable to a pointer variable. Pointer
variable can only contain address of a variable of the same data type.
& is used to determine the address of a variable.
#include<stdio.h>
void main()
{
int a = 10;
int *ptr; //pointer declaration
ptr = &a; //pointer initialization
}
Q.4 What do you understand by linked list? How is it represented? (CPU_WIN_2016)
A linked list is a dynamic data structure where each element (called a node) is made up of two
items - the data and a reference (or pointer) which points to the next node. A linked list is a
collection of nodes where each node is connected to the next node through a pointer. The first node
is called a head and if the list is empty then the value of head is NULL.
CKPCET, SURAT 3
PPS (3110003)
A linked list whose nodes contain two fields: an integer value and a link to the next node. The last
node is linked to a terminator used to signify the end of the list.
It is represented as
struct Node {
int data;
struct Node *next;
}
Q. 5 What is pointer ? Give its benefits. Write a program to do swapping of two elements
using function with two pointers as arguments. (CPU_SUM_2014)
Pointers are used to store and manage the addresses of dynamically allocated blocks of memory.
A pointer is a variable that contains an address which is a location of another variable in memory.
Since a pointer is a variable, its value is also stored in the memory in another location.
Benefits of Pointer.
A program to do swapping of two elements using function with two pointers as arguments.
#include<stdio.h>
int temp;
temp = *num1;
*num1 = *num2;
*num2 = temp;
CKPCET, SURAT 4
PPS (3110003)
int main() {
scanf("%d", &num1);
scanf("%d", &num2);
swap(&num1, &num2);
return (0);
Q. 6 Write a program to read n different integer numbers from keyboard and calculate the
sum using pointer. (CPU_SUM_2016)
Here, for example we read 5 integers from key board and calculate sum using pointer.
#include<stdio.h>
int main()
{
int array[5];
int i,sum=0;
int *ptr;
CKPCET, SURAT 5
PPS (3110003)
ptr = array;
for(i=0;i<5;i++)
{
//*ptr refers to the value at address
sum = sum + *ptr;
ptr++;
}
printf("\nThe sum is: %d",sum);
}
Output:
Enter array elements (5 integer values): 1 2 3 4 5
The sum is: 15
Q. 7 What is pointer? Which arithmetic operations are not valid on pointers?
(PPS_SUM_2019)
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;
Pointer arithmetic is not valid one. Pointer addition, multiplication and division are not allowed as
these are not making any sense in pointer arithmetic.
But, two pointers can be subtracted to know how many elements are available between these two
pointers.
Q. 8 What is pointer to pointer? Write suitable example to demonstrate the concept.
(PPS_SUM_2019)
Declaring Pointer to Pointer is similar to declaring pointer in C. The difference is we have to place
an additional ‘*’ before the name of pointer.
Syntax:
int **ptr;
Example :
CKPCET, SURAT 6
PPS (3110003)
#include <stdio.h>
return 0;
}
Q. 9 What care must be taken while writing a program with recursive function?
(PPS_SUM_2019)
A recursive algorithm must have a base case.
A recursive algorithm must change its state and move toward the base case.
A recursive algorithm must call itself, recursively.
the second law, we must arrange for a change of state that moves the algorithm toward the base
case. A change of state means that some data that the algorithm is using is modified. Usually the
data that represents our problem gets smaller in some way.
CKPCET, SURAT 7
PPS (3110003)
The final law is that the algorithm must call itself. This is the very definition of recursion.
Recursion is a confusing concept to many beginning programmers.
MCQ
(CPU_WIN_2019)
Q. Which of following operators is used to access the value of the variable using the pointer?
(a) * (b) ^ (c) % (d) &
(CPU_WIN_2017)
Q.What is the output of following C code?
int main()
{
int *ptr, a = 10;
ptr = &a;
*ptr += 1;
printf("%d,%d/n", *ptr, a);
}
a) 10,10 b) 10,11 c) 11,10 d) 11,11
(CPU_WIN_2016)
Q.A pointer value refers to
(a) A float value. (b) An integer constant. (c) Any valid address in memory. (d) None.
(CPU_WIN_2013)
Q. A Pointer is?
(A) A keyword used to create variables.
(B) A variable that stores address of an instruction.
(C) A variable that stores address of other variable.
(D) All of above.
CKPCET, SURAT 8
PPS (3110003)
(CPU_SUM_2014)
Q. A pointer value refers to
a) A float value b) An integer constant c) Any valid address in memory d)
None
(CPU_SUM_2016)
Q. If ptr is a pointer to int, having value ptr=100. After ptr++, what is the value of ptr?
(a) 100 (b) 101 (c) 102 (d) 103
(CPU_SUM_2018)
Q. Address stored in the pointer variable is of type _________
(a) Character (b) Integer (c) Floating (d) Array
CKPCET, SURAT 9