Question about the heap

+1 Daniel M · November 15, 2015
Hello, I saw the heap tutorial(#48) and I didnt understand why to use it.
I used this code and it has the same output.


    int i, howMany;
    int total = 0;
    float average = 0.0;

    printf("How many points do you want to average?\n");
    scanf(" %d", &howMany);

    int pointsArray[howMany];

    printf("Enter them! \n");

    for(i=0; i

What is the differences between the two? and what advantages the heap has?
Thanks

Post a Reply

Replies

Oldest  Newest  Rating
+1 Jon Z. · November 15, 2015
You need it for dynamic allocation. What if you don't know how many numbers will be entered?
Let's say the user enters numbers and then you add them all and print the sum, so you make an int array of size 10. What if the user wants to enter 11 numbers? So you make it bigger. An array of size 10000, but what if they want to enter 500000 numbers.
The solution is to use dynamic allocation. Just make the array small, and if more numbers need to be entered you make it bigger as needed.
  • 1

C

107,098 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator