1.CTSD-2 Unit - 1
1.CTSD-2 Unit - 1
Declaration of pointer :
syntax : datatype *pointername;
Example : int *ptr;
char *pt;
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.
Types of Pointers
1. Null Pointer
NULL Pointer is a pointer that is pointing to nothing(i.e. not pointing to any valid object
or memory location).
#include <stdio.h>
int main()
{
// Null Pointer
int * ptr = NULL;
return 0;
4. Dangling Pointer
• A dangling pointer in C refers to a pointer that points to a memory location that has been deallocated
or freed.
• This situation can lead to unexpected behavior and bugs in the program.
• Dangling pointers can occur in several scenarios, such as deallocation of memory, function calls, and
variables going out of scope.
• When a memory block pointed to by a pointer is deallocated using the free function, the pointer
becomes a dangling pointer.
4. Dangling Pointer
Pointers and Arrays
Array of pointers
Array of pointers
Pointer(accessing a variable through its pointer)
Pointer(accessing a variable through its pointer)
Declaration & Initialization of Pointer
Syntax
p is integer pointer variable
datatype *ptr_variablename; & is address of or referencing operator which
Example returns memory address of variable.
void main() * is indirection or dereferencing operator which
{ returns value stored at that memory address.
int a=10, *p; // assign memory address of a & operator is the inverse of * operator
to pointer variable p
p = &a;
x = a is same as x = *(&a)
printf("%d %d %d", a, *p, p);
}
Output
10 10 5000
Pointer to Pointer – Double Pointer
Pointer holds the address of another variable of same type.
When a pointer holds the address of another pointer then such type of
pointer is known as pointer-to-pointer or double pointer.
The first pointer contains the address of the second pointer, which
points to the location that contains the actual value.
Syntax Pointer Pointer Variable
datatype **ptr_variablename;
int **ptr;
address address value
Write a program to print variable, address of pointer variable and pointer to pointer variable.
#include <stdio.h>
int main () {
int var;
int *ptr;
int **pptr;
var = 3000;
ptr = &var; // address of var
pptr = &ptr; // address of ptr using address of operator &
printf("Value of var = %d\n", var );
printf("Value available at *ptr = %d\n", *ptr );
printf("Value available at **pptr = %d\n", **pptr);
return 0;
}
Output
Value of var = 3000
Value available at *ptr = 3000
Value available at **pptr = 3000
Relation between Array & Pointer – Cont.
Example: int a[10], *p;
a[0] is same as *(a+0), a[2] is same as *(a+2) and a[i] is same as *(a+i)
Syntax
datatype *name[size];
2018
int *ptr[5]; //declares an array of integer pointer of size 5
Array of Pointer – Cont.
An array of pointers ptr can be used to point to different rows of matrix as
follow:
for(i=0; i<5; i++)
{
ptr[i]=&mat[i][0];
}
return_type (*pointer_name)(parameter_types);
• In call by value method xerox copy of actual parameters is created and passed to the
called function.
• Any change made inside function will not affect the original value of variables inside
calling function.
Call By Value
Call By Value
Call by Reference in C
• In call by reference method of parameter passing, the address of the actual parameters is
passed to the function as the formal parameters. In C, we use pointers to achieve call-by-
reference.
• Both the actual and formal parameters refer to the same locations.
• Any changes made inside the function are actually reflected in the actual parameters of
the caller.
Call by Reference in C
Call by Reference in C
Disadvantages of pointers