0% found this document useful (0 votes)
9 views26 pages

Understanding Pointers in C Programming

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

Understanding Pointers in C Programming

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

C Programming II

Computer Science, XII


St. Xavier’s College
POINTERS

Computer Science
Pointer
▪ Also a variable.
▪ Stores the memory address of other variables of same
data type.
▪ Takes address of another variable as value.
Why use Pointer?
▪ Pointers save memory space.
▪ Execution time with pointers is faster because data
are manipulated with the address, i.e. direct access to
memory location.
▪ Memory is accessed efficiently with the pointers.
▪ Pointer can be used to assign and release the
memory.
Value and Address of a Variable

▪ a = 5;
▪ b = 6;
▪ ptr = &a;
Declaration of Pointer
Variable
▪ Must declare a pointer variable before using it.
▪ Syntax: data_type *pointer_name;
Initialization of Pointer

▪ To make a pointer variable point other variables by


providing address of that variable.
▪ Also known as referencing of pointer.
▪ Without referencing, pointer variables are meaningless.
▪ Syntax: pointer_variable = &normal_variable;
▪ pointer_variable and normal_variable must be of same
data types.
Initialization of Pointer
Address in C (Ampersand Operator)
▪ Ampersand (&) is used to
access the address of the
variable.
▪ Example: scanf(“%d”,
&a);
Indirection Operator (*)

▪ Used to access the value


of the variable pointed
by the pointers.
Working
of Pointer
Working
of Pointer
Working of Pointer
Things to
remember:
Sum of two
numbers
using
Pointer
Classwork

▪ Write a C program to calculate the area of a rectangle


using Pointer.
▪ Write a C program to find the maximum value
between two numbers using Pointer.
Pointer and Function
(Call by Reference)
▪ Passing the address of the variable to the
function instead of variable values.
Sum of two
numbers using
Call by Reference
Sum of two numbers using
Call by Reference
Classwork

▪ Write a C program to swap two numbers using Call by


Reference.
▪ Write a C program to calculate simple interest using Call by
Reference. Take user inputs.
▪ Write a C program to check whether the input number is
prime or not using Pointer and Function.
Difference between Call by Value & Reference
Call by Value Call by Reference
The parameter’s value is passed to invoke The parameter’s reference (address) is
the function. passed to invoke the function.
Changes occurring in any argument inside Any changes occurring in any argument
the function have no reflection on the inside the function also reflects in the passed
passed parameter. parameter.

The memory location of formal and actual The memory location of formal and actual
arguments are different. arguments are same.

There is no modification in the original value. There is modification in the original value.

It is supported by all programming Only C, JAVA supports call by reference.


languages.
Pointers with Array
Pointers
with Array
(Sum of
Numbers)
Advantages

▪ Enables to use call by reference mechanism.


▪ Efficient use of memory with Dynamic Memory
Management.
▪ Manipulation of arrays and strings through pointer makes
the efficient program.
▪ Advanced data structures like linked lists, trees, graphs, etc
can be created and stored.
Disadvantages
▪ Complex to understand.
▪ Might cause segmentation fault.
○ When a program tries to read or write an illegal memory
location.
▪ If an invalid value is provided to a pointer, it may cause memory
corruption.
▪ Responsible for memory leakage. (Dynamically allocated
memory)
▪ Programmer needs to manipulate pointer carefully.
THANK YOU!

Computer Science THE END

You might also like