开发者

Pointer incompatible type error

int main()
{
double x = 10.08;
double *p;
p = &x;
printf("value of pointer is 开发者_StackOverflow社区%d\n",p);
*p=&x;//error line
printf("value of x, *p = %lf\n",x);
return 0;
}

when i do it for int type(i.e. int x, int *p), its working fine. But not for double, why?


*p has type double; &x has type double *. They are incompatible.

By the way, to print out pointer values, use the conversion %p:

printf("value of pointer is %p\n", p);


*p is of type double. &x is of type pointer-to-double. Why do you expect them to be compatible?

I expect your compiler, if you turn on (and read) the warnings, to complain about that even with ints.


Replace

 *p = &x                         with     *p = (unsigned int) &x  \\ this works
 *p = &x it means x = &x ; Not a syntax to follow

Though this is not to be followed in c because *p ,it inturns means x value and you wanna store the address in the x value but you did not declare it as the pointer but just a normal variable.

however with the int you dont get any error but a warning indicating that

 non portable assigment 

I dont know the deep roots but as for my investigation,i guess the pointers save the address in some other format but show us the integer format

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

But i highly recommend not to follow The above syntax.

people dont use double pointer always Instead go for void pointer

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜