[go: up one dir, main page]

0% found this document useful (0 votes)
13 views33 pages

1.CTSD-2 Unit - 1

The document provides an overview of pointers in C programming, including their definition, advantages, and various types such as null, wild, void, and dangling pointers. It discusses how pointers can be used with arrays, as function arguments, and the concepts of call by value and call by reference. Additionally, it highlights the disadvantages of pointers, such as complexity and potential for memory corruption.

Uploaded by

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

1.CTSD-2 Unit - 1

The document provides an overview of pointers in C programming, including their definition, advantages, and various types such as null, wild, void, and dangling pointers. It discusses how pointers can be used with arrays, as function arguments, and the concepts of call by value and call by reference. Additionally, it highlights the disadvantages of pointers, such as complexity and potential for memory corruption.

Uploaded by

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

303105151 - Computational

Thinking for Structured Design-2

Dr. Harish Prajapati, Assistant Professor


Computer Science & Engineering
CHAPTER-1
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.
Basics of 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.
Dereferencing operator(*) used to declare pointer variable and access the value stored in the
address.
Address operator(&) used to returns the address of a variable or to access the address of a
variable to a pointer.

 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;

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
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
#include <stdlib.h>
#include <stdio.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
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)

a: a[0] a: *(a+0) 2000


a[1] *(a+1) 2002
a+1:
. .
a: . .
. .
. .
a[i] a+i: *(a+i) 2000 + i*2
. .
. .
. .
. .
a[9] a+9: *(a+9) 2018
Array of Pointer
 As we have an array of char, int, float etc, same way we can have an
array of pointer.
 Individual elements of an array will store the address values.
 So, an array is a collection of values of similar type. It can also be a
collection of references of similar type known by single name

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];
}

By dynamic memory allocation, we do not


require to declare two-dimensional array, it can
be created dynamically using array of pointers
2018
Function Pointer in C
In C, a function pointer is a type of pointer that stores the address of a function,
allowing functions to be passed as arguments and invoked dynamically.
Function Pointer Declaration

return_type (*pointer_name)(parameter_types);

return_type: The type of the value that the function returns.


parameter_types: The types of the parameters the function takes.
pointer_name: The name of the function pointer.

int (*fpr)(int, int);


Function Pointer in C
Example:.
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
// Declare a function pointer that matches
// the signature of add() fuction
int (*fptr)(int, int);
// Assign to add()
fptr = &add;
// Call the function via ptr
printf("%d", fptr(10, 5));
return 0;
}
Call by Value

• 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

•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