使用指针编译C程序时出错

use*_*end 0 c pointers

当我在我的计算机上运行它时,我得到编译错误.但是,我确实从我在互联网上找到的教程中直接复制了它.

#include <stdio.h>
#include <conio.h>

void main(){
    int i = 9;
    clrscr();

    printf("The value of i is: %d\n", i);
    printf("The address of i is: %u\n", &i);
    printf("The value at the address of i is: %d\n", *(&i));

    getch();
}
Run Code Online (Sandbox Code Playgroud)

错误:

$ cc "-Wall" -g    ptrex6.c   -o ptrex6
ptrex6.c:7:19: error: conio.h: No such file or directory
ptrex6.c:9: warning: return type of ‘main’ is not ‘int’
ptrex6.c: In function ‘main’:
ptrex6.c:11: warning: implicit declaration of function ‘clrscr’
ptrex6.c:14: warning: format ‘%u’ expects type ‘unsigned int’, but argument 2 has type ‘int *’
ptrex6.c:17: warning: implicit declaration of function ‘getch’
make: *** [ptrex6] Error 1
Run Code Online (Sandbox Code Playgroud)

小智 7

误区:

  1. conio.h不是标准的C头.它可能在您的系统上不可用.然而,printf()不需要它.这就是为什么stdio.h在这里.删除它,并删除clrscr().没有conio库,它将无法工作.通过这样做,您将能够编译您的文件,因为其他消息是"只是"警告,而不是错误.

  2. main()函数的返回类型更改为int并返回0.这就是C标准指定的内容.你要这个.

  3. 使用%d格式说明符代替%u.正如编译器消息直接指出的那样,%u用于无符号整数,并且int是显式签名的.对于整数> = 2 ^ 31,您将遇到奇怪的行为问题.

  4. 您再次使用错误的说明符.使用%p的地址/指针,而不是%u /%d /不管.

  5. 不要明确地相信/复制粘贴教程.教程不适用于复制粘贴,而是要考虑和学习它们.

  • 在当前标准中,它等同于返回"0"并且在C89中返回一个未指定值的"int". (2认同)