int main()
{
double x = 10.08;
double *p;
p = &x;
printf("value of pointer is %d\n",p);
*p=&x;//error line
printf("value of x, *p = %lf\n",x);
return 0;
}
当我这样做的int类型(即int x,int * p),它的工作正常。但不是双重的,为什么?指针不兼容类型错误
int main()
{
double x = 10.08;
double *p;
p = &x;
printf("value of pointer is %d\n",p);
*p=&x;//error line
printf("value of x, *p = %lf\n",x);
return 0;
}
当我这样做的int类型(即int x,int * p),它的工作正常。但不是双重的,为什么?指针不兼容类型错误
*p
有型号double
; &x
有型号double *
。它们不兼容。
顺便提及,打印出的指针值,使用转换%p
:
printf("value of pointer is %p\n", p);
*p
的类型为double。 &x
是指向双精度型的类型。你为什么期望它们兼容?
我希望你的编译器,如果你打开(并阅读)的警告,抱怨,即使与int
s。
更换
*p = &x with *p = (unsigned int) &x \\ this works
*p = &x it means x = &x ; Not a syntax to follow
虽然这不是因为* P,它inturns应遵循c表示x值,你想保存在x值的地址,但你没有声明为指针,但只是一个正常的变量。
但是与诠释你没有得到任何错误,但表明
non portable assigment
我不知道根基深厚,但对于我的调查,我猜指针保存的地址在其他一些格式,但显示警告我们的整数格式
int x;
int *p;
p = &x ;
*p = &x ; warning non portable assignment replace with *p = (unsigned int) &x
using typecasting (unsigned int) coverts the address format to integer format
I guess So . May be the address format and integer format are different
Thats why you cant assign address to a variable unless it is typecasted
but pointers dont need them because as they are invented to store the address
This is what i think from my investigation
但我强烈建议不要遵循上面的语法。
人们不总是使用双指针而是去虚空指针
为什么我得到-1 MR downvoter – niko
我是一个总C newb。这就是说,你遇到了问题,因为在'* p =&x'中,你正在引用'p'并将它分配给一个指向'x'的指针。我不相信这与'int'和'double'有什么关系 – 2011-09-09 05:44:55
你认为那行代码要做什么? (提示:你可能是错的,反思指针,声明一个指针和使用一元'*'运算符的区别) –
你的意思是* p = x;在错误线上?你是否试图将x的值存储到p指向的地址? (void *)p);' –