这里是 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 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);
这是我的输出
最佳答案
double array = malloc(sizesizeof(int));
应该是:
double *array = malloc( size * sizeof(double) );
scanf("%d", &array[i]);
这里的控制字符串是错误的, double 应该是%lf
,而不是%d
关于c - 数组内容无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35518686/