C Program Using Dynamic Memory Allocation Function


Top C Program Using Dynamic Memory Allocation

C Program Using Dynamic Memory Allocation Function

C Program Using malloc () function in Dynamic Memory Allocation Function.

Or

C Program Using malloc () and free () function in Dynamic Memory Allocation Function.

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

void main()

{

int num, i, *ptr, add = 0;

printf("Enter number of elements:");

scanf("%d", &num);

ptr = (int*) malloc(num * sizeof(int));   

if(ptr == NULL)

{

printf("Error! Memory is not allocated.");

exit(0);

}

printf("Enter elements: ");

for(i = 0; i < num; ++i)

{

scanf("%d", ptr + i);

add =add+ *(ptr + i);

}

printf("Add = %d", add);

free(ptr);

getch();

}

Output:

Enter number of elements:3

Enter elements:

1

2

3

Add = 6

C Program Using calloc () function in Dynamic Memory Allocation Function.

Or

C Program Using calloc () and free () function in Dynamic Memory Allocation Function.

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

void main()

{

int num, i, *ptr, add = 0;

printf("Enter number of elements: ");

scanf("%d", &num);

ptr = (int*) calloc(num, sizeof(int));

if(ptr == NULL)

{

printf("Error! Memory is not allocated.");

exit(0);

}

printf("Enter elements: ");

for(i = 0; i < num; ++i)

{

scanf("%d", ptr + i);

add =add+ *(ptr + i);

}

printf("add = %d", add);

free(ptr);

getch();

}

Output:

Enter number of elements: 5

Enter elements:

1

2

3

4

5

add = 15

C Program Using realloc () function in Dynamic Memory Allocation Function.

Or

C Program Using calloc () and free () function in Dynamic Memory Allocation Function.

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

Void main()

{

int *ptr, i , m1, m2;

printf("Enter the size of list: ");

scanf("%d", &m1);

ptr = (int*)malloc(m1 * sizeof(int));

printf("Display the address of previously allocated memory: ");

for(i = 0; i < m1; ++i)

printf("%u\n",ptr + i);

printf("\nEnter the new size of list: ");

scanf("%d", &m2);

ptr = realloc(ptr, m2 *sizeof(int));             

printf("Display the address newly allocated memory: ");

for(i = 0; i <m2; ++i)

printf("%u\n", ptr + i);

free(ptr);

getch();

}

Output:

Enter the size of list: 5

Display the address of previously allocated memory: 3753921216

3753921220

3753921224

3753921228

3753921232

 

Enter the new size of list: 3

Display the address of newly allocated memory: 3753921216

3753921220

3753921224

C Program storing element and finding the largest element in the array list using calloc () function in Dynamic Memory Allocation Function.

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

void main()

 {

int number, i;

float *n;

printf("Enter the total number of elements in the list: ");

scanf("%d", &number);

n = (float *)calloc(number, sizeof(float));

if (n == NULL)

{

printf("Error!!! Memory is not allocated.");

exit(0);

}

for (i = 0; i < number; ++i)

{

printf("Enter Number %d: ", i + 1);

scanf("%f", n + i);

}

for (int i = 1; i < number; ++i)

{

if (*n < *(n + i))

*n = *(n + i);

}

printf("Largest number = %.2f", *n);

getch();

}

 

Output:-Enter the total number of elements in the list: 5

Enter Number 1: 1

Enter Number 2: 2

Enter Number 3: 3

Enter Number 4: 4

Enter Number 5: 5

Largest number = 5.00

 



 

Comments

Popular posts from this blog

C Program to print swap number using third variables

C Program to open and write to a text file using fprintf.