我正在测试此代码,但为什么没有错误呢?没有错误,为什么?
#include <stdio.h>
int main()
{
int a = 1025;
int *p;
p = &a;
// now I declare a char variable ,
char *p0;
p0 = (char*) p; // type casting
printf("", sizeof(char));
// is %d correct here ?????
printf("Address = %d, value = %d\n", p0, *p0);
}
我的问题: 是%d
正确的吗?因为%d
是整数不是字符,为什么没有错误呢?
其实问题是你传递地址的第一个'%d':你应该使用'%p'。 'printf(“Address =%p,value =%d \ n”,(void *)p0,* p0);'对于第二个'char'值只是提升为'int'。 – LPs
此外,错误/警告还取决于您如何编译代码:使用'gcc -Wall -Wextra -pedantic-errors test.c -o test -std = c11 -g',您有'warning':format'%d'需要参数类型'int',但参数2的类型为'char *'[-Wformat =] printf(“Address =%d,value =%d \ n”,p0,* p0);' – LPs
[格式正确说明符打印指针(地址)?](http://stackoverflow.com/q/9053658/995714) –