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

Arrays and Dynamic Memory Allocation

The document explains the use of dynamic memory allocation in C using 'malloc' and 'calloc' functions, detailing their syntax and providing examples for allocating single and multi-dimensional arrays. It includes code snippets demonstrating how to allocate memory, check for successful allocation, initialize arrays, and calculate sums of array elements. Additionally, it describes how to create a 2D array of pointers and initialize it with addresses from another array.

Uploaded by

colabpython39
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Arrays and Dynamic Memory Allocation

The document explains the use of dynamic memory allocation in C using 'malloc' and 'calloc' functions, detailing their syntax and providing examples for allocating single and multi-dimensional arrays. It includes code snippets demonstrating how to allocate memory, check for successful allocation, initialize arrays, and calculate sums of array elements. Additionally, it describes how to create a 2D array of pointers and initialize it with addresses from another array.

Uploaded by

colabpython39
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

The “malloc” or “memory allocation” method in C is used to

dynamically allocate a single large block of memory with the specified


size. It returns a pointer of type void which can be cast into a pointer of
any form. It initializes each block with default garbage value.

Syntax: ptr = (cast-type*) malloc(byte-size)

Example:
int *ptr;
ptr = (int*) malloc(100 * sizeof(int));
#include <stdio.h>
#include <stdlib.h>
int main() {
int* ptr;
int n, i;
n = 5;
printf("Enter number of elements: %d\n", n);
ptr = (int*)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
else {
printf("Memory successfully allocated using malloc.\n");
for (i = 0; i < n; ++i) {
ptr[i] = i + 1;
}
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
}
free(ptr);
return 0;
}
The “calloc” or “contiguous allocation” method in C is used to
dynamically allocate the specified number of blocks of memory of the
specified type. It initializes each block with a default value ‘0’.

Syntax: ptr = (cast-type*)calloc(n, element-size);

Example:
float *ptr;
ptr = (float*) calloc(25, sizeof(float));
#include <stdio.h>
#include <stdlib.h>
int main() {
int* ptr;
int n, i;
n = 5;
printf("Enter number of elements: %d\n", n);
ptr = (int*)calloc(n, sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
else {
printf("Memory successfully allocated using calloc.\n");
for (i = 0; i < n; ++i) {
ptr[i] = i + 1;
}
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
}
free(ptr);
return 0;
}
//sum of array elements using pointers
#include <stdio.h>
#include <malloc.h>
int main() {
int i, n, sum = 0;
int *ptr;
printf("Enter size of array : ");
scanf("%d", &n);
ptr = (int *)malloc(n * sizeof(int));
printf("Enter elements in the List ");
for (i = 0; i < n; i++) {
scanf("%d", ptr + i);
}
//calculate sum of elements
for (i = 0; i < n; i++) {
sum = sum + *(ptr + i);
}
printf("\n Sum of all elements in an array is = %d\n", sum);
free(ptr);
return 0;
A two-dimensional array of pointers can also be created using
Dynamic Memory Allocation. We can use the malloc() function to
dynamically allocate memory.
#include <stdio.h>
#include <stdlib.h>
int main() {
int arr1[5][5] = { { 0, 1, 2, 3, 4 },
{ 2, 3, 4, 5, 6 },
{ 4, 5, 6, 7, 8 },
{ 5, 4, 3, 2, 6 },
{ 2, 5, 4, 3, 1 } };

// Creating 2D array of pointers using Dynamic Memory


allocation through malloc() function
int*** arr2 = (int**)malloc(5 * sizeof(int**));
for (int i = 0; i < 5; i++)
arr2[i] = malloc(5 * sizeof(int*));
// Initialising each element of the pointer array with the address of
element present in the other array
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
arr2[i][j] = &arr1[i][j];
}
}
// Printing the array using the array of pointers
printf("The values are\n");
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
printf("%d ", *arr2[i][j]);
sum=sum+*arr2[i][j];
}
printf("\n");
printf("Sum of two dimensional array elements is %d\n", sum);
}
free(arr2);
return 0;
}
How to create a 2D array of pointers: A 2D array of pointers can be
created following the way shown below.
int *arr[5][5]; //creating a 2D integer pointer array of 5 rows and 5
columns.
The element of the 2D array is been initialized by assigning the address
of some other element.

#include <stdio.h>
int main() {
int arr1[5][5] = { { 0, 1, 2, 3, 4 },
{ 2, 3, 4, 5, 6 },
{ 4, 5, 6, 7, 8 },
{ 5, 4, 3, 2, 6 },
{ 2, 5, 4, 3, 1 } };

int* arr2[5][5];
// Initializing each element of the pointer array with the address of
element present in the other array
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
arr2[i][j] = &arr1[i][j];
}
}
// Printing the array using the array of pointers
printf("The values are\n");
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
printf("%d ", *arr2[i][j]);
}
printf("\n");
}
return 0;
}

You might also like