c - 数组内容无法正确显示

标签 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 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

最佳答案

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/

相关文章:

c - 为什么我在 getaddrinfo() 返回的列表中得到 "localhost"的重复 addrinfo 对象?

c - 从 c 中的 char* float

创建自定义安装程序

c - 动态字符串输入并在 C 中对它们进行排序

javascript - 调整 keyup 事件以在用户完成输入后调用 API

python - 如何授予输入权限以接受带空格的名称?

c - C编译器在递归过程中如何处理堆栈中的局部变量?

php - 每四个值分隔数组并使 MySql 插入

java - java中如何将文件放入数组中

javascript - 在里面插入一个动态值