Week 6
Week 6
Week 6
POINTERS
Pointers in C are used to store the address of variables or a memory location. This variable
can be of any data type i.e, int, char, function, array, or any other pointer. The pointer of type
void is called Void pointer or Generic pointer. Void pointer can hold address of any type of
variable. The size of the pointer depends on the computer architecture like 16-bit, 32-bit, and
64-bit.
Syntax:
datatype *var_name;
Let pointer “ptr” holds the address of an integer variable or holds the address of memory
whose value(s) can be accessed as integer values through “ptr”. So to define “ptr” we can do
it in four ways, which are as following:
int *ptr;
int* ptr;
int * ptr;
int*ptr;
Example:
void geeks()
{
int var = 20;
// Driver program
int main()
{
geeks();
return 0;
Output
Value at ptr = 0x7ffd15b5deec
Value at var = 20
Value at *ptr = 20
Uses of pointer
1. To pass arguments by reference
2. For accessing array elements
3. To return multiple values
4. Dynamic memory allocation
5. To Implement data structures
6. To do System-Level Programming where memory addresses are useful
7. To help locating exact value at exact location.
8. To avoid compiler confusion for same variable name.
9. To use in Control Tables.
int main()
{
int x;
// Prints address of x
printf("%p", &x);
return 0;
}
Output
0x7fffd60dddfc
2. One more operator is unary or dereference operator * (Asterisk) which is used for two
things:
2. a. To declare a pointer variable: When a pointer variable is declared in C/C++, there must
be a * before its name.
return 0;
}
2. b. To access the value stored in the address we use the unary operator (*) that returns the
value of the variable located at the address specified by its operand. This is also
called Dereferencing.
int main()
{
// A normal integer variable
int Var = 10;
// This prints 20
printf("After doing *ptr = 20, *ptr is %d\n", *ptr);
return 0;
}
Output
Value of Var = 10
Address of Var = 0x7ffd11cd52ac
After doing *ptr = 20, *ptr is 20
Advantages of Pointers
• Pointers are used for dynamic memory allocation and deallocation.
• An Array or an 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 reduces length of the program and its execution time as well.
Disadvantages of pointers
• Memory corruption can occur if an incorrect value is provided to pointers
• Pointers are Complex to understand.
• Pointers are majorly responsible for memory leaks.
• Pointers are comparatively slower than variables in C.
• Uninitialized pointers might cause segmentation fault.
Void Pointers
A Void Pointer in C can be defined as an address of any variable. It has no standard data
type. A void pointer is created by using the keyword void.
int main()
{
// void pointer
void* ptr = NULL;
return 0;
}
Output
The size of pointer is:8
NULL Pointers
Null pointers can be created by assigning a zero value during pointer declaration. This
method is useful when no address is assigned to the pointer.
Syntax:
int * ptr = NULL;
Example:
int main()
{
// null pointer
int* ptr = NULL;
return 0;
}
Output
The value inside variable ptr is:
0
Wild Pointers
Wild Pointers are pointers that have not been initialized with something yet. These types of
C-pointers can cause problems in our programs and can eventually cause them to crash.
While working with Wild Pointers Programmer must be very careful.
int main()
{
// wild pointer
int* ptr;
printf("\n%d", *ptr);
return 0;
}
Output:
timeout: the monitored command dumped core
/bin/bash: line 1: 32 Segmentation fault timeout 15s ./543664c1-f84d-4498-ba9b-
4f209538f96a < 543664c1-f84d-4498-ba9b-4f209538f96a.in
Other types of pointers in C are as follows:
• Far pointer – A far pointer is typically 32 bit that can access memory outside current
segment.
• Dangling pointer – A pointer pointing to a memory location that has been deleted (or
freed) is called dangling pointer.
• Huge pointer – A huge pointer is 32 bit long containing segment address and offset
address.
• Complex pointer – Pointers with multiple levels of indirection.
• Near pointer – Near pointer is used to store 16 bit addresses means within current
segment on a 16 bit machine.
• Normalized pointer – It is a 32 bit pointer, which has as much of its value in the
segment register as possible.
• Generic pointer – In C void* acts as a generic pointer.
• File Pointer – The pointer to a FILE data type is called as a stream pointer or a file
pointer.