0% found this document useful (0 votes)
6 views

Ctsd2 Unit 1

The document provides an overview of pointers in programming, detailing their definition, advantages, types, and usage in accessing variables and memory management. It explains concepts such as pointer declaration, initialization, dereferencing, and the differences between call by value and call by reference. Additionally, it discusses various pointer types, including null, wild, void, and dangling pointers, along with their implications and potential errors associated with their use.

Uploaded by

jeetshinde6453
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Ctsd2 Unit 1

The document provides an overview of pointers in programming, detailing their definition, advantages, types, and usage in accessing variables and memory management. It explains concepts such as pointer declaration, initialization, dereferencing, and the differences between call by value and call by reference. Additionally, it discusses various pointer types, including null, wild, void, and dangling pointers, along with their implications and potential errors associated with their use.

Uploaded by

jeetshinde6453
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Computational Thinking for

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.

2. Address operator(&) used to returns the address of a variable or to


access the address of a variable to a pointer.
Pointer
Pointer
A. Pointer Declaration
To declare a pointer, we use the (*) dereference operator before its name. In
pointer declaration, we only declare the pointer but do not initialize it.
B. Pointer Initialization
Pointer initialization is the process where we assign some initial value to the
pointer variable. We use the (&) addressof operator to get the memory
address of a variable and then store it in the pointer variable.
C. Pointer Dereferencing
Dereferencing a pointer is the process of accessing the value stored in the
memory address specified in the pointer. We use the same (*) dereferencing
operator that we used in the pointer declaration.
Advantages of Pointers
• Pointers make the programs simple and reduce their length.
• Pointers are helpful in allocation and de-allocation of memory during the
execution of the program. Thus, pointers are the instruments of
dynamic memory management.
• Pointers enhance the execution speed of a program.
• Pointers are helpful in traversing through arrays and character strings.
The strings are also arrays of characters terminated by the null character
(‘\O’).
• Storage of strings through pointers saves memory space.
• Pointers may be used to pass on arrays, strings, functions, and variables
as arguments of a function.
Types of Pointers

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;

printf("The value of ptr is %p", ptr);


return 0;
}
2. Wild Pointer
• Wild pointers, also known as random pointers, point to arbitrary memory
locations and can cause a program to behave differently.
#include <stdio.h>
int main()
{
int* p; /* wild pointer */
// trying to access the value pointed by a wild pointer
// is undefined behavior
// printf("Value pointed by wild pointer: %d\n", *p);
// //give error
int x = 10;
// Accessing the value pointed by 'p'
printf("Value pointed by 'p' is: %d\n", *p);
return 0;}
3. Void Pointer
• A void pointer is a pointer without an associated data type. It can store the
address of any type and can be retrieved to any type.
• Void pointer is a specific pointer type – void * – a pointer that points to some
data location in storage, which doesn’t have any specific type.
3. Void Pointer
EXAMPLE:
#include <stdlib.h>
int main()
{
int x = 4;
float y = 5.5;
void* ptr;
ptr = &x;
printf("Integer variable is = %d", *((int*)ptr));
// void pointer is now float
ptr = &y;
printf("\nFloat variable is = %f", *((float*)ptr));

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

You might also like