You are on page 1of 13

Pointers in c

Presented by : Rozeep Rai


Table of contents:

Introduction 

Declaration and syntax

Pointer operator

Why are pointer used ?

Pointer with array 

Advantages and limitaion  of pointer 

Conclusion 
What is Pointer ?

A pointer is defined as a derived data type


that can store the address of other C
variables or a memory location. 

The data stored in the memory location


can be access and manipulate by using
pointer.
*Declaration and syntax :
• The general form of declaring a pointer variable in 'c' programming is :

Data_type *pointer_varibale 

Data type must be a valid C data type and pointer_variable is the name of pointer variable. The (*) sign
is used to signify the varaiable as the pointer variable.
The pointer special operator;

  &  - returns the memory address of the operand

 *     - It is the complement of & . It returns the value contained in the memory


location pointed to by the pointer variables value.
Adding two numbers using
pointers  
#include <stdio.h>
int main()
{
   int first, second, *p, *q, sum;
   printf("Enter two integers to add\n");
   scanf("%d%d", &first, &second);
      p = &first;
   q = &second;
   sum = *p + *q;
   printf("Sum of the numbers = %d\n", sum);
   return 0;
}
Why are pointer used ?

• To return more than one value from a function.


• To pass array & string more conveniently from one
function to another.
• To manipulate arrays more easily by moving pointers
to them, Instead of moving the array themselves.
• To allocate memory and access it 
• To create complex data structures such as linked list.
Pointers with Arrays 

The address of an array element can be expressed in two ways:


• By writing the actual array element preceded by the ambersand (&) sign.
• By writing an expression in which the subscript is added to the array name.
Advantages of pointers:
• Following are the major advantages of pointers in C:

• Pointers are used for dynamic memory allocation and deallocation.

• An Array or a structure can be accessed efficiently with pointers

• Pointers are useful for accessing memory locations.

• Pointers are used to form complex data structures such as linked lists, graphs, trees, etc.

• Pointers reduce the length of the program and its execution time as well.
Disadvantages of pointers:

Memory corruption
Pointer syntax can be can occur if an Requires a null
complex incorrect value is pointer references
provided to pointers

Uninitialized pointers
Slower than variables
Memory leaks in C might cause a
in C
segmentation fault
Conclusion

In conclusion, pointers are a fundamental and


powerful concept in the C programming language
which  provide a way to directly interact with
memory addresses, enabling various critical
functionalities such as memory management,
efficient data manipulation, and dynamic data
structures.
References:
• What is SRP or single responsibility principle example in java? solid design pattern example (no date) Javarevisited. Available at:
https://javarevisited.blogspot.com/2017/04/single-responsibility-principle-example.html#axzz88sHMUrSP (Accessed: 29 July 2023).

• https://www.learnpick.in/prime/documents/ppts/details/106/pointers-in-c
Thank you

You might also like