CREATED BY K. VICTOR BABU
COMPUTATIONAL THINKING FOR
STRUCTURED DESIGN
Topic:
POINTERS AND
DYNAMIC MEMORY ALLOCATION
Department of BES-1
Session – 23
CREATED BY K. VICTOR BABU
AIM OF THE SESSION
To familiarize students with
Pointers , Character pointers and DMA
INSTRUCTIONAL OBJECTIVES
This Session is designed to:
1. Know about pointers, their advantages and disadvantages
2. Learn about pointer initialization and memory allocation
3. To create character pointers and access strings by using them.
4. Understand the difference between character pointers and character arrays.
5. Learn about usage of pointers to store the value of the string.
6. Perform various operations on string by using character pointers.
7. Understanding of DMA
LEARNING OUTCOMES
At the end of this session, you should be able to:
1. Learn how to declare and initialize the pointers.
2. Create character pointers
3. Know the difference between character pointers and character arrays.
4. Perform string handling by using character pointers
5. Create Dynamic memory at runtime.
CREATED BY K. VICTOR BABU
INTRODUCTION
• Understanding Pointers
• Pointers advantages and disadvantages.
• Initialization of Pointer Variables.
• Accessing the Address of a Variable
• Accessing a variable through its pointer.
• Create character pointer to string in C
• How to use pointers to store the value of string in C program.
• Use character pointers to perform various string handling functions
KLEF P-1(CTSD) BES-1
CREATED BY K. VICTOR BABU
Session Description
• A pointer in C is a
 Variable that stores the memory address of another variable.
Access to Variables by using the address of that variable.
 Pointers are used to manipulate memory and create more efficient
programs.
• Declaring a pointer:
datatype *pointervariablename;
int *pnum;
char *pchar;
float *pfloat;
KLEF P-1(CTSD) BES-1
CREATED BY K. VICTOR BABU
10
x
ptr
2000
Name of the cell(Variable)
Value stored in cell (Data/Information)
Address of Variable x
9000
Name of the cell(Variable)
Value stored in cell (Data/Information)
ptr=&x
Address of Variable ptr
CREATED BY K. VICTOR BABU
Integer Pointer?
A special type of variable to store address of another variable. An integer pointer is declared by
using int keyword and * (asterisk) and only points to integer variable.
int x = 10;
int *ptr;
int ptr = &x; //address of x is assigned to pointer ptr.
10
x
20002
ptr
20010
10
x
20002
ptr
pointed to
Address of pointer ptr
Address of variable x
address 20002
stored
KLEF P-1(CTSD) BES-1
20002
So pointer points variable like this.
CREATED BY K. VICTOR BABU
#include <stdio.h>
int main()
{
int a, b, *pa, *pb, sum;
a=10;
b=20;
pa = &a;
pb = &b;
sum = *pa + *pb;
printf("Sum of the numbers = %dn", sum);
return 0;
}
KLEF P-1(CTSD) BES-1
10 20
a b
2000 4056
2000 4056
pa pb
7000 2030
30
sum
9012
CREATED BY K. VICTOR BABU
&a is read as “address of var”.
*fptr is read as “value at ptr”.
Address of first cell of an array is called base address of that array. Float array element can be accessed using float
pointer by assigning base address to float pointer and adding cell index.
0 1 2 3 4
a[0] a[1] a[2] a[3] a[4]
3.14 -0.465 8.5 4.5 33.6
float *fptr=&a;
fptr
*(fptr + 1); // equivalent to a[1]
fptr[2]; // equivalent to a[2]
KLEF P-1(CTSD) BES-1
CREATED BY K. VICTOR BABU
Character Pointers
• In C language, character pointers are used to store the memory
address of a character or a sequence of characters.
• A character pointer can be declared using the following
Syntax:
Ex: char *ptr;
• A character pointer can also be used to declare and initialize a string
literal.
Ex: char *str = "Hello, world!";
KLEF P-1(CTSD) BES-1
CREATED BY K. VICTOR BABU
• To access the characters in the string, we can use pointer arithmetic
with the character pointer:
char *ptr = "Hello, world!";
printf("%cn", *ptr);
// Output: H
ptr++; // Increment the pointer to point to the next character
printf("%cn", *ptr);
// Output: e
KLEF P-1(CTSD) BES-1
CREATED BY K. VICTOR BABU
• We can also use character pointers to manipulate strings and
character arrays.
Ex: The strcpy() function can be used to copy a string from one
character array to another using pointers:
• char source[] = "This is the source string.";
char destination[50];
char *ptr = source;
strcpy(destination, ptr);
printf("%sn", destination);
// Output: This is the source string.
KLEF P-1(CTSD) BES-1
CREATED BY K. VICTOR BABU
ACTIVITIES/ CASE STUDIES/ IMPORTANT FACTS RELATED TO THE
SESSION
• You are working on a program that involves searching for a particular
substring within a larger string using character pointers. Write a
function that takes two pointers to character arrays as input: one
representing the larger string and the other representing the
substring to be searched for. The function should return a pointer to
the first occurrence of the substring within the larger string, or NULL
if the substring is not found.
KLEF P-1(CTSD) BES-1
CREATED BY K. VICTOR BABU
Dynamic Memory Allocation
C is a structured language, it has some fixed rules for programming. One of them includes changing the size of an array.
An array is a collection of items stored at contiguous memory locations.
• CZZVZDFGSG
Ablockofm
em
orycanbeallocatedusingthefunctionm
alloc.
• Reservesablockofmemoryofspecifiedsizeandreturnsapointeroftypevoid.
• Thereturnpointercanbetype-castedtoanypointertype.
G
eneralform
at:
ptr= (type*)m
alloc(byte_size);
CREATED BY K. VICTOR BABU
calloc()
The name "calloc" stands for contiguous allocation.
The malloc() function allocates memory and leaves the memory uninitialized, whereas the calloc() function allocates
memory and initializes all bits to zero.
• Syntax of calloc()
• ptr = (castType*)calloc(n, size);
Example:
ptr = (float*) calloc(25, sizeof(float));
The above statement allocates contiguous space in memory for 25 elements of type float.
free()
Dynamically allocated memory created with either calloc() or malloc() doesn't get freed on their own. You must
explicitly use free() to release the space.
•Syntax of free()
•free(ptr);
This statement frees the space allocated in the memory pointed by ptr.
CREATED BY K. VICTOR BABU
Sometimesweneedtoalterthesizeofsomepreviouslyallocatedmemoryblock.
• M
orem
em
oryneeded.
• Memoryallocatedislargerthannecessary.
How?
• Byusingthereallocfunction.
Iftheoriginalallocationisdoneas:
ptr = malloc (size);
thenreallocationofspacem
aybedoneas:
ptr = realloc (ptr, newsize);
CREATED BY K. VICTOR BABU
EXAMPLES
• Let's see the example of malloc() function &calloc() function.
CREATED BY K. VICTOR BABU
REFERENCES FOR FURTHER LEARNING OF THE SESSION
Reference Books:
1. Mark Allen weiss, Data Structures and Algorithm Analysis in C, 2008, Third Edition, Pearson Education.
2. Horowitz, Sahni, Anderson Freed, “Fundamentals of Data structures in C”, 2nd Edition2007.
3. Robert Kruse, C. L. Tondo, Bruce Leung, Shashi Mogalla, “Data structures and Program Design in C”, 4th Edition-2007
Sites and Web links:
1. www.hackerrank.com
2. www.codechef.com
Reference Books:
1. 1.) Brian W. Kernighan, Dennis M. Ritchie, “The C Programming Language: ANSI C Version”, 2/e, Prentice-Hall/Pearson Education-2005.
2.) E. Balagurusamy, “Programming in ANSI C” 4thed. Tata McGraw-Hill Education, 2008
Sites and Web links:
1) https://www.w3schools.com/c/c_arrays.php
2) https://www.includehelp.com/c-programs/c-programs-one-dimensional-arrays-problems-and-solutions.aspx
https://technictiming.com/c-programming/array/