If so, why do I need to allocate manually any way?
Try the following program:
Code:
#include <stdio.h>
int main(void)
{
int scores[50]; /* How big should this be??? */
int num_scores;
int i;
printf("How many scores (1 - 50)? ");
fflush(stdout);
scanf("%d", &num_scores);
for(i = 0;i < num_scores;++i)
scanf("%d", &scores[i]);
return 0;
}
How big are you going to make the array? One person might just want to enter 10, another person might want to enter 5000. What are you going to do? Make the array as big as possible to make sure there's enough room? That would definitely be a waste of memory if someone only wanted to enter a few.
Dynamic allocation to the rescue!
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *scores;
int num_scores;
int i;
printf("How many scores (1 - 50)? ");
fflush(stdout);
scanf("%d", &num_scores);
if(!(scores = malloc(sizeof(*scores) * num_scores)))
{
puts("Memory allocation error!");
return 1;
}
for(i = 0;i < num_scores;++i)
scanf("%d", &scores[i]);
free(scores);
return 0;
}