Ctsd2 Unit 1
Ctsd2 Unit 1
Structured Design-II(303105151)
Mrs. Praptiba Solanki
Computer Science & Engineering Department (PIT)
CHAPTER-1
POINTERS
Contents
• Pointers:
• Basics of pointers
• Advantages of pointers
• accessing a variable through its pointer
• pointers and arrays
• array of pointers
• pointer to pointer
• pointer as a function argument
• call by value & call by reference
• Dangling Pointer.
Pointer
A pointer is a variable that stores the memory address of another variable.
Instead of holding a direct value, it holds the address where the value is stored
in memory. There are 2 important operators that we will use in pointers
concepts i.e.
1. Dereferencing operator(*) used to declare pointer variable and access the
value stored in the address.
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
Other Pointers
5. Complex pointer
A complex pointer consists of [], *, (), an identifier and a datatype. These operators
contain different levels of associativity and precedence. [] and () have the highest
precedence and these are specified from left to right.
char(* ptr)[2]
6. Near pointer
A Near Pointer stores 16-bit addresses, which means it can only access memory
within the current data on a 16-bit machine.
7. Far pointer
A Far Pointer uses 16-bit registers to store addresses, which allows access to enable
the current segment. Each compiler assigns one register for the segment and
another for the offset within the segment.
Pointers and Arrays
Array of pointers
Array of pointers
Pointer(accessing a variable through its pointer)
Pointer(accessing a variable through its pointer)
Pointer to Pointer (Double Pointer)
• double pointers are those pointers which stores the address of another pointer.
The first pointer is used to store the address of the variable, and the second pointer
is used to store the address of the first pointer. That is why they are also known as
a pointer to pointer.
• A double pointer can be declared similar to a single pointer. The difference is we
have to place an additional ‘*’ before the name of the pointer.
• type **name;
Pointer to Pointer (Double Pointer)
Call by Value
• In call by value method xerox copy of actual parameters is created and passed to
the called function.
• 2. 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 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
Call by Reference
Advantages of pointers
• 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
• Pointers are vulnerable to errors and have following disadvantages:
• Memory corruption can occur if an incorrect value is provided to pointers.
• Pointers are a little bit complex to understand.
• Pointers are majorly responsible for memory leaks in C.
• Pointers are comparatively slower than variables in C.
• Uninitialized pointers might cause a segmentation fault.
www.paruluniversity.ac.in