Static data structure
Dynamic data structure
Linear data structure
Non-linear data structure
Let's quickly brush up the concept of pointers:
Syntax ↓
ptr = (cast-type *) malloc(size of memory)
Example ↓
int *ptr;
ptr = (int *) malloc(100 * sizeof(int));
Since the size of int is 4 bytes, this statement will allocate 400 bytes of memory.
And, the pointer ptr holds the address of the first byte in the allocated memory.
we are using sizeof() because in different architecture data-type value changes
Syntax ↓
ptr = (cast-type *) calloc(n, element-size);
here, n is the no. of blocks and element-sze is the size of each element.
For example ↓
ptr = (float *) calloc(25, sizeof(float));
This statement allocates contiguous space in memory for 25 elements each with the size of the float.
Syntax ↓
ptr = realloc(ptr, newSize);
here ptr is reallocated with new size 'newSize'.
#include <stdio.h>
#include <stdlib.h>
int main()
{
// use of malloc
int *ptr;
int n;
printf("Enter the size of the array you want to create : ");
scanf("%d", &n);
ptr = (int *)malloc(n * sizeof(int)); // create dynamic array of 10 size
// Check if the memory has been successfully allocated by malloc or not
if (ptr == NULL)
{
printf("Memory not allocated.\n");
exit(0);
}
else
{
for (int i = 0; i < n; i++)
{
printf("Enter the value no %d of this : ", i);
scanf("%d", &ptr[i]);
}
for (int i = 0; i < n; i++)
{
printf("The value %d of this array is %d\n", i, ptr[i]);
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr;
int n, i;
printf("Enter the number of elements : ");
scanf("%d", &n);
// Dynamically allocate memory using calloc()
ptr = (int *)calloc(n, sizeof(int));
// check if the memory has been successfully allocated by calloc or not
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;
}
// print the elements of the array
printf("The elements of the array are: ");
for (i = 0; i < n; i++)
{
printf("%d\t", ptr[i]);
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr;
int n, i, previousSize;
printf("Enter the number of elements : ");
scanf("%d", &n);
// Dynamically allocate memory using calloc()
ptr = (int *)calloc(n, sizeof(int));
// check if the memory has been successfully allocated by calloc or not
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;
}
// print the elements of the array
printf("The elements of the array are: ");
for (i = 0; i < n; i++)
{
printf("%d\t", ptr[i]);
}
previousSize = n;
printf("\nEnter the new size of the array : ");
scanf("%d", &n);
// Dynamically re-allocate memory using realloc()
ptr = realloc(ptr, n * sizeof(int));
printf("Memory successfully re-allocated using realloc\n");
// initializing new element array with new value
for (i = previousSize; i < n; i++)
{
ptr[i] = i + 1;
}
// print the elements of the array
printf("The elements of the array after using realloc are: ");
for (i = 0; i < n; i++)
{
printf("%d\t", ptr[i]);
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr1, *ptr2;
int n, i;
printf("Enter the number of elements : ");
scanf("%d", &n);
// Dynamically allocate memory using malloc()
ptr1 = (int *)malloc(sizeof(int));
// Dynamically allocate memory using calloc()
ptr2 = (int *)calloc(n, sizeof(int));
// check if the memory has been successfully allocated by calloc or not
if (ptr1 == NULL || ptr2 == NULL)
{
printf("Memory not allocated.\n");
exit(0);
}
else
{
printf("Memory successfully allocated using malloc.\n");
// free ptr1 memory
free(ptr1);
printf("Malloc memory successfully freed.\n");
printf("Memory successfully allocated using calloc.\n");
// free ptr2 memory
free(ptr2);
printf("Calloc memory successfully freed.\n");
}
return 0;
}
References ↓