C 数组内容未正确显示
C初学者在这里,我有一个程序,我正在工作,不幸的是,该程序不是在数组中正确读取,就是没有正确显示内容 这是我的C 数组内容未正确显示,c,arrays,input,output,C,Arrays,Input,Output,C初学者在这里,我有一个程序,我正在工作,不幸的是,该程序不是在数组中正确读取,就是没有正确显示内容 这是我的 int main() { int size; // the number of elements printf("Enter size of the array: \n"); scanf("%d", &size); double *array = malloc(size*sizeof(int)); //memory allocat
int main()
{
int size; // the number of elements
printf("Enter size of the array: \n");
scanf("%d", &size);
double *array = malloc(size*sizeof(int)); //memory allocated using malloc
if (array == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements of array: \n");
for (int i = 0; i<size; ++i)
{
scanf("%d", &array[i]);
}
printf("Results:");
double min = array[0];
printf("\nmin = %.3lf ", min);
double max = array[size - 1];
printf("\nmax = %.3lf", max);
这是我的输出
双数组=mallocsizesizeofint
应该是:
double *array = malloc( size * sizeof(double) );
扫描%d,&数组[i]
这里错误的控制字符串,应该是%lf表示double,而不是%d这样做了,同样的事情仍然在发生,您正在分配一个整数数组并将其分配给一个双指针。