C - 分配动态数组后无法接受输入

标签 c arrays dynamic

我需要有人告诉我我在这里做错了什么。

void main(void) {
    int *arr,n,i;

    printf("Enter the number of elements you want to enter=");
    scanf("%d",&n);

    arr = (int*)calloc(n,sizeof(int));

    if(!arr)
        return;

    for(i=0;i<n;i++)
        scanf("%d",&arr[i]);

    for(i=0;i<n;i++)
        printf("%d ",arr[i]);

    free(arr);
}

这只是一个简单的程序,用于输入数组中的项目数,然后获取输入并打印输入的值。但是,每当我在从用户那里获取项目数后在数组中输入值时,我的程序就会崩溃。我不明白我在这里做错了什么。立即需要一些帮助。请运行该程序并查看它在其他系统上是否也崩溃。我使用在线编译器执行了该程序,奇怪的是它运行良好。我正在使用 Visual Studio 2013 命令行编译器来执行我的程序。

最佳答案

gcc编译这段代码我得到:

calloc.c: In function ‘main’:
calloc.c:4:5: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
calloc.c:5:5: warning: incompatible implicit declaration of built-in function ‘scanf’ [enabled by default]
calloc.c:7:17: warning: incompatible implicit declaration of built-in function ‘calloc’ [enabled by default]
calloc.c:18:5: warning: incompatible implicit declaration of built-in function ‘free’ [enabled by default]

使用clang:

calloc.c:1:1: error: 'main' must return 'int'
void main(void) {
^
calloc.c:4:5: warning: implicitly declaring C library function 'printf' with type 'int (const char *, ...)'
    printf("Enter the number of elements you want to enter=");
    ^
calloc.c:4:5: note: please include the header <stdio.h> or explicitly provide a declaration for 'printf'
calloc.c:5:5: warning: implicitly declaring C library function 'scanf' with type 'int (const char *, ...)'
    scanf("%d",&n);
    ^
calloc.c:5:5: note: please include the header <stdio.h> or explicitly provide a declaration for 'scanf'
calloc.c:7:17: warning: implicitly declaring C library function 'calloc' with type 'void *(unsigned long, unsigned long)'
    arr = (int*)calloc(n,sizeof(int));
                ^
calloc.c:7:17: note: please include the header <stdlib.h> or explicitly provide a declaration for 'calloc'
calloc.c:18:5: warning: implicit declaration of function 'free' is invalid in C99 [-Wimplicit-function-declaration]
    free(arr);
    ^
4 warnings and 1 error generated.

正如 clang 善意指出的那样,您需要添加

#include <stdlib.h>
#include <stdio.h>

到代码顶部,因为您正在使用在代码中声明的函数。您正在使用的函数位于标准 C 库中,因此代码仍然可以编译,但您应该预料到意外情况。程序崩溃的原因可能是编译器对 calloc 的返回类型做出了错误的假设,因为它没有正确的声明。许多系统会让您逃脱惩罚,但至少它们应该警告您。

附注,请参阅this discussion强制转换 malloc 的返回值(不强制转换)。此建议也适用于 calloc

<小时/>

编辑

这是一个可能阐明该问题的最小示例:

a.c

#include <stdio.h>

int main()
{
    printf("%f\n", f());
    return 0;
}

b.c

float f()
{
    return 4.2;
}

这两个文件可以分别编译成目标文件:

gcc -c a.c      # creates a.o
gcc -c b.c      # creates b.o

不会生成任何警告或错误。切换到 C99 模式会导致“隐式函数声明”警告。请注意,在编译时,不需要函数定义。这两个目标文件可以链接:

gcc a.o b.o

现在,链接器找到了函数的定义,因此没有错误,但已经太晚了。编译 a.c 时的 f() 隐式声明会导致程序运行时输出不正确(我得到 0.000000)。

这里发生了与你的问题相同的事情。可以找到函数calloc,因此链接时没有错误。但是,对它返回的类型做出了错误的假设,因此它在您的系统上的行为不符合预期。

关于C - 分配动态数组后无法接受输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23138405/

相关文章:

flash - 如何基于原始WAV/DATA或其他Sound对象动态创建新的Sound对象?

c - 使用简单的链接器脚本给出 "relocation truncated to fit: R_X86_64_32S"

c - 段错误 - 施特拉森矩阵乘法

c - C语言动态内存链表语法错误

arrays - 除非显式引用一个字典枚举键,否则编译器会报错

c - 如何在 C 中实现 next() PHP 函数

c - 如何为自引用结构类型的对象的成员赋值?

C 如何捕获关闭终端

c++ - 当我们在c中使用fopen()搜索文件时,实现了哪种搜索技术

arrays - 如何: multidimensional arrays in vhdl