Hello everyone I am working on a c program for a class and I am having issues with calloc. I am using calloc to create a dynamic array and then trying to store elements into that array after initialization. However, after I run my code everytime, I only have zeros in the array.
Code:
/* Chapter8Homework#3 */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int x = 0, k = 0;
float *array_size;
printf("Please enter a number for the size of an array: ");
scanf("%d", &x);
array_size = (float *)calloc(x, sizeof(float));
for(k=0;k<x;k++)
{
printf("Enter a number: ");
scanf("%f", array_size + k);
}
for(k=0;k<x;k++)
{
printf("The number at element %d is %f\n", k, array_size + k);
}
return 0;
}
If anyone could help me out that would be much appreciated. I am really quite stuck because it seems to not make sense. Any help would be awesome. Thanks in advance!