当我在我的计算机上运行它时,我得到编译错误.但是,我确实从我在互联网上找到的教程中直接复制了它.
#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
误区:
conio.h
不是标准的C头.它可能在您的系统上不可用.然而,printf()不需要它.这就是为什么stdio.h
在这里.删除它,并删除clrscr().没有conio库,它将无法工作.通过这样做,您将能够编译您的文件,因为其他消息是"只是"警告,而不是错误.
将main()
函数的返回类型更改为int
并返回0.这就是C标准指定的内容.你要这个.
使用%d
格式说明符代替%u
.正如编译器消息直接指出的那样,%u用于无符号整数,并且int
是显式签名的.对于整数> = 2 ^ 31,您将遇到奇怪的行为问题.
您再次使用错误的说明符.使用%p
的地址/指针,而不是%u /%d /不管.
不要明确地相信/复制粘贴教程.教程不适用于复制粘贴,而是要考虑和学习它们.
归档时间: |
|
查看次数: |
2668 次 |
最近记录: |