0% found this document useful (0 votes)
26 views

Lecture 3.1.1 Basics of Pointers

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Lecture 3.1.1 Basics of Pointers

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

UNIVERSITY INSTITUTE OF ENGINEERING

DEPARTMENT- ACADEMIC UNITS


FIRST YEAR ENGINEERING PROGRAMMES
Subject Name : Introduction to Problem Solving
Subject Code: 24CSH-101

Pointers: Basics DISCOVER . LEARN . EMPOWER


Introduction to
Problem Solving

Course Objectives

The course aims to provide exposure to problem-


solving through programming.

The course aims to raise the programming skills


of students via logic building capability.

With knowledge of C programming language,


students would be able to model real world
problems. 2
Course
Outcomes
CO Course Outcome
Number

CO1 Remember the concepts related to fundamentals of C language,


draw flowcharts and write algorithm/pseudocode.

CO2 Understand the way of execution and debug programs in C lan-


guage.
CO3 Apply various constructs, loops, functions to solve mathematical
and scientific problem.

CO4 Analyze the dynamic behavior of memory by the use of pointers.

CO5 Design and develop modular programs for real world problems us-
ing control structure and selection structure.

3
ASSESSMENT PATTERN
The performance of students is evaluated as follows:

Theory Practical
Continuous Internal As- Semester End Examina- Continuous Internal Semester End Exam-
Components sessment (CAE) tion (SEE) Assessment (CAE) ination (SEE)

Marks 40 60 60 40
Total Marks 100 100

4
Introduction
• POINTER is a variable that stores address
of another variable.
• A pointer can also be used to refer to
another pointer function.
• A pointer can be
incremented/decremented, i.e., to point
to the next/ previous memory location.
• The purpose of pointer is to save Source: https://www.geeksforgeeks.org/double-pointer-pointer-pointer-c/
memory space and achieve faster
execution time.

5
Features of Pointers
• Pointers save memory space.
• Execution time with pointers is faster because data are manipulated with the address,
that is, direct access to memory location.
• Memory is accessed efficiently with the pointers. The pointer assigns and releases the
memory as well. Hence it can be said the Memory of pointers is dynamically allocated.
• Pointers are used with data structures. They are useful for representing two-
dimensional and multi-dimensional arrays.
• An array, of any type can be accessed with the help of pointers, without considering its
subscript range.
• Pointers are used for file handling.
• Pointers are used to allocate memory dynamically.
• In C++, a pointer declared to a base class could access the object of a derived class.
However, a pointer to a derived class cannot access the object of a base class.
6
Declaring a pointer
• Like variables, pointers have to be declared before they can be used in
your program.
• Pointers can be named anything you want as long as they obey C's
naming rules.
• A pointer declaration has the following form.
data_type * pointer_variable_name;
Here,
• data_type is the pointer's base type of C's variable types and indicates the
type of the variable that the pointer points to.
• The asterisk (*: the same asterisk used for multiplication) which is
indirection operator, declares a pointer.
7
Initialize a pointer
• After declaring a pointer, we initialize it like standard variables with a variable
address.
• If pointers are not uninitialized and used in the program, the results are
unpredictable and potentially disastrous.
• To get the address of a variable, we use the ampersand (&)operator, placed
before the name of a variable whose address we need.
• Pointer initialization is done with the following syntax:
pointer = &variable;

8
Example:
#include <stdio.h>
int main()
{
int a=10; //variable declaration
int *p; //pointer variable declaration
p=&a; //store address of variable a in pointer p
printf("Address stored in a variable p is:%x\n",p); //accessing the address
printf("Value stored in a variable p is:%d\n",*p); //accessing the value
return 0;
}

Output:
Address stored in a variable p is:60ff08
Value stored in a variable p is:10
9
Operators used with Pointers
Operator Meaning

* 1.Serves 2 purposeDeclaration of a pointer


2.Returns the value of the referenced variable

& •Serves only 1 purpose


•Returns the address of a variable

10
Types of a pointer
1. Null pointer
We can create a null pointer by assigning null value during the pointer declaration. This method is
useful when you do not have any address assigned to the pointer. A null pointer always contains value
0.
Following program illustrates the use of a null pointer:
#include <stdio.h>
int main()
{
int *p = NULL; //null pointer
printf(“The value inside variable p is:\n%x”,p);
return 0;
}
Output:
The value inside variable p is:
0
11
2. Void Pointer
In C programming, a void pointer is also called as a generic pointer. It does not have
any standard data type. A void pointer is created by using the keyword void. It can
be used to store an address of any variable.

Following program illustrates the use of a void pointer:


#include <stdio.h>
int main()
{
void *p = NULL; //void pointer
printf("The size of pointer is:%d\n",sizeof(p));
return 0;
}

Output:
The size of pointer is:4
12
3. Wild pointer
A pointer is said to be a wild pointer if it is not being initialized to anything. These
types of pointers are not efficient because they may point to some unknown memory
location which may cause problems in our program and it may lead to crashing of the
program. One should always be careful while working with wild pointers.
Following program illustrates the use of wild pointer:
#include <stdio.h>
int main()
{
int *p; //wild pointer
printf("\n%d",*p);
return 0;
}
Output:
timeout: the monitored command dumped core
sh: line 1: 95298 Segmentation fault timeout 10s main

13
Uses of pointers
• To pass arguments by reference
• For accessing array elements
• To return multiple values
• Dynamic memory allocation
• To implement data structures
• To do system level programming where memory addresses are useful

14
Disadvantages of pointer
• Uninitialized pointers might cause segmentation fault.
• Dynamically allocated block needs to be freed explicitly.
• If pointers are updated with incorrect values, it might lead to memory
corruption.

15
Summary

A pointer can be
POINTER is a variable that A pointer can also be used
incremented/decremented,
stores address of another to refer to another pointer
i.e., to point to the next/
variable. function.
previous memory location.

The purpose of pointer is to


save memory space and Memory is accessed
achieve faster execution efficiently with the pointers.
time.

16
Frequently Asked question
Q1:2) What will be the output of the C program?
#include<stdio.h>
int main(){
int i = 3;
int *j;
j = &i;
j++;
printf("%d ",*j);
return 0;
}
Output:

17
2) What will be the output of the C program?
#include<stdio.h>
int main(){
int a = 130;
char *ptr;
ptr = (char *)&a;
printf("%d ",*ptr);
return 0;
}
Ans : -126

18
• Output:

19
Q3) What is “&” and “*” operators in C?
Ans:
1. “*” Operator is used as pointer to a variable. Example: * a where * is
pointer to the variable a.
2. & operator is used to get the address of the variable. Example: &a will give
address of a.

Q4) WHAT IS VOID POINTER IN C?


Ans:
3. Void pointer is a generic pointer that can be used to point another variable of
any data type.
4. Void pointer can store the address of variable belonging to any of the data type.
So, void pointer is also called as general purpose pointer.

20
Assessment Questions:
1. C Program to calculate Area of Circle using Pointer

2. Write a C Program to Swap Two Numbers / Variables using Pointer?

3. What will be the output of the C program?


#include<stdio.h>
#include<string.h>
int main(){
char *ptr = "hello";
char a[22];
strcpy(a, "world");
printf("\n%s %s",ptr, a);
return 0;
}
21
Discussion forum.
• Other types of pointers in 'c' are as follows:
1. Dangling pointer
2. Complex pointer
3. Near pointer
4. Far pointer
5. Huge pointer

• Write a program to count the number of words, lines and characters in a text using
pointer

22
REFERENCES
Reference Books
[1] Programming in C by Reema Thareja.
[2] Programming in ANSI C by E. Balaguruswamy, Tata McGraw Hill.
[3] Programming with C (Schaum's Outline Series) by Byron Gottfried Jitender
Chhabra, Tata McGraw Hill.
[4] The C Programming Language by Brian W. Kernighan, Dennis Ritchie, Pearson
education.

Websites:
1. https://www.geeksforgeeks.org/features-and-use-of-pointers-in-c-c/
2. https://www.guru99.com/c-pointers.html

YouTube Links:
3. https://www.youtube.com/watch?v=sY-s7O0FiYE&t=3s
4. https://www.youtube.com/watch?v=qcWQ1AmmNWI&t=193s

23
THANK YOU

You might also like