UrbanPro
true

Learn Programming Languages from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Dynamic Memory Allocation in C using malloc()

Sankhadeep Pujaru
04/06/2020 0 0

#include <stdio.h>
#include <conio.h>
//#include <malloc.h> OR
#include <stdlib.h>
void main()
{
    int *ptr, i, n, sum = 0;
    printf("how many elements ? ");
        scanf("%d", &n);
    ptr = (int *)malloc(n * sizeof(int));   


  if (ptr == NULL)
    printf("\n Memory not allocated.\n");
  else
  {
    printf("\nEnter the elements : \n");
      for (i=0; i<n; i++)
      {
        scanf("%d", (ptr+i));
        sum += *(ptr + i);        // sum will calculate the summation of n numbers
      }
printf("\n\nSum = %d, and elements : ", sum);
for (i=0; i<n; i++)
    printf("\n %d", *(ptr + i));
}

free(ptr);   //deallocates the memory previously allocated by a call to malloc()

printf("\n\nSum = %d, and elements : ", sum);
  for (i = 0; i<n; i++)
    printf("\n\n %d", *(ptr + i));    // it will print garbage values as we have deallocated the memory                                                            // using free() call
getch();
}

0 Dislike
Follow 2

Please Enter a comment

Submit

Other Lessons for You

Bit wise operators in C
Bit Wise Operators Bit Wise operators are manipulates of individual bits with in a word of memory. The bit wise operators can be divided in to three general category. One’s Complement...

Design your own Mouse Driver in C Language
Mouse Driver (msdrv.h) #include #include union REGS i,o; restrictmouseptr(int x1, int y1, int x2, int y2) { i.x.ax=7; i.x.cx=x1; i.x.dx=x2; ...

Some interview questions for freshers on C language
Q1. What are the two forms of #include? There are two variants of #include. The one is #include and the other one is #include”file”. In general the first form that is #include is used to...

Be prepared to get trained--init
Before starting the training,students must be mentally prepared for acceptance of new knowledge. Students must attend training with open minded forgetting the position they are working.This will help...
S

Smartnub Softsolutions

0 0
0

Set 0 To Kth Bit In A Variable
The following code snippet Set 0 to Kth Bit in an variable #include int UnsetBitValue(int n, int k);int main(){ printf("%d\n\n",UnsetBitValue(255,6)); return 0;}// Set kth bit to zeroint UnsetBitValue(int...
X

Looking for Programming Languages Classes?

The best tutors for Programming Languages Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Programming Languages with the Best Tutors

The best Tutors for Programming Languages Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more