CP_Module-4b_ppt-2023-24
CP_Module-4b_ppt-2023-24
Pointers
● Fundamentals of pointers
● Declaration, initialization and dereferencing of pointers
● Operations on Pointers
● Concept of dynamic memory allocation
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. We can access and manipulate the data stored in
that memory location using pointers. As the pointers store the memory addresses,
their size is independent of the type of data they are pointing to. This size of pointers
only depends on the system architecture.
Syntax
- The syntax of pointers is similar to the variable declaration in C, but we use the ( * )
dereferencing operator in the pointer declaration.
datatype * ptr;
- where
● ptr is the name of the pointer.
● datatype is the type of data it is pointing to.
- The above syntax is used to define a pointer to a variable. We can also define
pointers to functions, structures, etc.
How to use pointers?
The use of pointers can be divided into three steps:
1. Pointer Declaration
2. Pointer Initialization
3. Dereferencing
How to use pointers?
1. Pointer Declaration
- In pointer declaration, we only declare the pointer but do not initialize it. To
declare a pointer, we use the ( * ) dereference operator before its name.
Example:
int *ptr;
- The pointer declared here will point to some random memory address as it is
not initialized. Such pointers are called wild pointers.
How to use pointers?
2. Pointer Initialization
- Pointer initialization is the process where we assign some initial value to the pointer
variable. We generally use the ( & ) addressof operator to get the memory
address of a variable and then store it in the pointer variable.
- Example int var = 10;
int * ptr;
ptr = &var;
- We can also declare and initialize the pointer in a single step. This method is called
pointer definition as the pointer is declared and initialized at the same time.
- Example
int *ptr = &var;
Note: It is recommended that the pointers should always be initialized to some
value before starting using it. Otherwise, it may lead to number of errors.
How to use pointers?
3. Dereferencing
1)pointer reduces the code and improves the performance, it is used to retrieving
strings, trees, etc. and used with arrays, structures, and functions.
3) It makes you able to access any memory location in the computer's memory.
Example on Pointer
Output
#include <stdio.h>
void geeks()
int* ptr;
ptr = &var;
// Driver program
int main() {
geeks();
return 0;
Operations on pointers
1) Pointer assignment:
p1=p2;
2) Pointer Arithmetic
- There are only two operations that you may use on pointers: addition and subtraction.
Suppose p1 is an integer pointer with the current value 6000.
- Makes p1 point to the tenth element of p1’s type beyond the one it currently points to. If
integer is 2 byte long, the news value of p1 will be 6020 and not 6010.
- Will increment p1. The new value of p1 will be 6002 and not 6001.
The expression, p1 - - ;
2) Pointer Arithmetic
y=*p1 * *p2;
*p2=*p2+10;
Output
Pointers and Functions:
Example:
Program to swap two numbers using call by
address (reference)
Output
Dynamic memory allocation in C
As it can be seen that the length (size) of the array above made is 9. But what if there is a
requirement to change this length (size). For Example,
- If there is a situation where only 5 elements are needed to be entered in this array. In this
case, the remaining 4 indices are just wasting memory in this array. So there is a
requirement to lessen the length (size) of the array from 9 to 5.
- Take another situation. In this, there is an array of 9 elements with all 9 indices filled. But
there is a need to enter 3 more elements in this array. In this case, 3 indices more are
required. So the length (size) of the array needs to be changed from 9 to 12.
Dynamic memory allocation in C
- This procedure is referred to as Dynamic Memory Allocation in C.
- Therefore, C Dynamic Memory Allocation can be defined as a procedure in which
the size of a data structure (like Array) is changed during the runtime.
- C provides some functions to achieve these tasks. There are 4 library functions
provided by C defined under <stdlib.h> header file to facilitate dynamic memory
allocation in C programming. They are:
1. malloc()
2. calloc()
3. free()
4. realloc()
Dynamic memory allocation in C
- Before learning above functions, let's understand the difference between static
memory allocation and dynamic memory allocation.
A quick look at the methods used for dynamic memory allocation.
Dynamic memory allocation in C
1. malloc() method
- The malloc() function allocates single block of requested memory.
- It doesn't initialize memory at execution time, so it has garbage value initially.
- It returns NULL if memory is not sufficient.
Syntax of malloc() in C
For Example:
2. calloc() method
- “calloc” or “contiguous allocation” method in C is used to dynamically allocate the
specified number of blocks of memory of the specified type. it is very much similar to
malloc() but has two different points and these are:
- If memory is not sufficient for malloc() or calloc(), you can reallocate the memory by
realloc() function. In short, it changes the memory size.
- Let's see the syntax of realloc() function.
ptr=realloc(ptr, new-size)
Dynamic memory allocation in C
4. free() method
free(ptr)