Lecture-SP13-Array-3
Lecture-SP13-Array-3
Structured Programming
Lecture 13
Array in C (3)
Prepared by________________________________
Md. Mijanur Rahman, Prof. Dr.
Dept. of Computer Science and Engineering
Jatiya Kabi Kazi Nazrul Islam University, Bangladesh
www.mijanrahman.com
Contents
A R R AY I N C
• C Array
• Properties of Array
• Advantage and disadvantage of C Array
• Declaration and Initialization of Array
• C Array Example
• Two Dimensional Array in C
• Character Arrays and Strings
• Dynamic Array
Array in C 2
Dynamic Array
• A dynamic array, also known as a resizable array or growable array, is a data structure
that allows elements to be stored in contiguous memory locations and provides the
ability to resize the array dynamically during program execution.
Array in C 3
How Dynamic Array Works?
• Here's how a typical dynamic array works:
1. Initialization: Initially, a dynamic array is created with a certain capacity (or initial
size).
2. Adding Elements: When an element is added to the array, it dynamically allocates
a new block of memory.
3. Accessing Elements: Elements in a dynamic array are typically accessed using
indices, just like in static arrays.
4. Resizing: Dynamic arrays resize themselves automatically when necessary.
5. Removing Elements: Similarly, when elements are removed from a dynamic array,
the array may shrink its capacity to save memory.
Array in C 4
How Dynamic Array Works?
• Initialization:
• Initially, a dynamic array is created with a certain capacity (or initial size). This capacity is
usually larger than the number of elements currently stored in the array to accommodate future
growth without needing to resize the array frequently.
• Adding Elements:
• When an element is added to the dynamic array and the current capacity is exceeded, the
array dynamically allocates a new block of memory with a larger capacity (often doubling the
current capacity is a common strategy). Then, it copies the existing elements into the new
memory block and inserts the new element. This process is called resizing or reallocating the
array.
Array in C 5
How Dynamic Array Works?
• Accessing Elements:
• Elements in a dynamic array are typically accessed using indices, just like in static arrays. Since
elements are stored in contiguous memory locations, accessing elements by index is efficient.
• Resizing:
• Dynamic arrays resize themselves automatically when necessary, ensuring that there is enough
space to accommodate additional elements.
• However, resizing operations can be relatively expensive because they involve allocating new
memory, copying elements, and deallocating the old memory block. To minimize the frequency
of resizing and the associated overhead, dynamic arrays often increase their capacity by a
certain factor (e.g., doubling the capacity) each time they need to resize.
Array in C 6
How Dynamic Array Works?
• Removing Elements:
• When elements are removed from a dynamic array, and the number of elements
becomes significantly smaller than the current capacity, the array may shrink its capacity
to save memory.
• However, many dynamic array implementations do not shrink the capacity immediately
to avoid frequent resizing operations due to minor fluctuations in the number of
elements.
Array in C 7
Dynamic Array Implementation in C
• In C, dynamic arrays are typically implemented using pointers and dynamic memory
allocation functions like malloc(), realloc(), and free().
• Unlike static arrays whose size is fixed at compile time, dynamic arrays allow you to
allocate memory dynamically at runtime, enabling you to resize them as needed.
Array in C 8
Dynamic Array Implementation in C
1. Allocation:
• To create a dynamic array, you first allocate memory for the array using the malloc()
function. For example:
int *dynamicArray = malloc(initialSize * sizeof(int));
• This allocates memory for an array of initialSize integers and returns a pointer to the
first element of the array. If the allocation is successful, dynamicArray will point to the
beginning of the allocated memory block.
Array in C 9
Dynamic Array Implementation in C
2. Accessing Elements:
• Elements in the dynamic array can be accessed using array notation or pointer
arithmetic, just like in static arrays. For example:
dynamicArray[0] = 10; // Assigning a value to the first element
int element = dynamicArray[0]; // Accessing the value of the first element
Array in C 10
Dynamic Array Implementation in C
3. Resizing:
• To resize the dynamic array to accommodate more elements, we can use the realloc() function.
This function allows to change the size of the previously allocated memory block. For example:
dynamicArray = realloc(dynamicArray, newSize * sizeof(int));
• This reallocates memory for the dynamic array to accommodate newSize elements. It may
either resize the existing memory block or allocate a new block and copy the existing elements
to it, depending on the available memory and the implementation of realloc().
• The contents of the original array are preserved up to the minimum of the old and new sizes.
Array in C 11
C Program using Dynamic array:
A simple C program that demonstrates
the use of dynamic arrays to store
integers.
Array in C 12
C Program using Dynamic array:
This program demonstrates dynamic
memory allocation to store names
entered by the user.
Array in C 13
C Program using Dynamic array:
Array in C 14
C Program using Dynamic array:
Array in C 15
?
Array in C
THE END
16