数组内容显示不正确

问题描述:

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); 

,这里是我的输出

enter image description here

+0

你分配一个整数数组并将其分配给一个双指针。 –

阵列=的malloc(大小的sizeof(int)的);

应该是:

double *array = malloc(size * sizeof(double)); 

的scanf( “%d”,&阵列[I]);

这里不对控制线,应该是%lf双,而不是%d

+0

是不是这样,同样的事情还在发生 – Frankjoww

+0

哇,真的很简单,谢谢 – Frankjoww