I am a student studying for a test next week on Dynamic Memory Allocation in C. I am looking at old test questions and have come across one that says to:
Write a C program that dynamically creates a single dimension array of length 30,000 and initialize the array with random numbers from 0 to 9.
I have a solid understanding of how to create the random numbers and place them into an array but I am confused about my malloc statement and how to place numbers other than the 'sizeof' code inside of it. For example, I have this code that allocates memory for a float:
#include<stdio.h>
#include<stdlib.h>
float* func();
int main(void)
{
int i;
float* x;
float array[0];
x=func();
srand(time(NULL));
i=random()%10;
array[i];
}
float * func(void)
{
float z;
float* ptr;
ptr = malloc(30000 * sizeof(float));
if(ptr == NULL)
{
puts("ALLOCATION FAILED.\n");
}else{
ptr = &z;
return ptr;
}
}
My question is how can i dynamically allocate memory for a single dimension array of length 30,000? What sort of things would this allocation be useful for for say a real world software development job?