警告:从不兼容的指针类型分配
#include #include #include int main() { double x,y; printf("Enter a double: "); scanf("%lf", &x); printf("Enter another double:"); scanf("%lf", &y); int *ptr_one; int *ptr_two; ptr_one = &x; ptr_two = &x; int *ptr_three; ptr_three = &y; printf("The Address of ptr_one: %p \n", ptr_one); printf("The Address of ptr_two: %p \n", ptr_two); printf("The Address of ptr_three: %p", ptr_three); return 0; }
问题是我每次尝试提交此代码时都会显示以下消息:
从不兼容的指针类型分配[默认启用]
我认为math.h
可能会解决它但是消极的。 请修理它需要帮助
这里:
ptr_one = &x; ptr_two = &x; ptr_three = &y;
ptr_one
, ptr_two
和ptr_three
是int*
s, &x
和&y
是double*
s。 您正在尝试使用double*
分配int*
double*
。 因此警告。
通过将指针类型更改为double*
而不是int*
来修复它,即更改以下行
int *ptr_one; int *ptr_two; int *ptr_three;
至
double *ptr_one; double *ptr_two; double *ptr_three;
以下变化应该做到
#include #include #include int main() { double x,y; printf("Enter a double: "); scanf("%lf", &x); printf("Enter another double:"); scanf("%lf", &y); double *ptr_one; // HERE double *ptr_two; // HERE ptr_one = &x; ptr_two = &x; double *ptr_three; // HERE ptr_three = &y; printf("The Address of ptr_one: %p \n", (void *)ptr_one); // HERE printf("The Address of ptr_two: %p \n", (void *)ptr_two); // HERE printf("The Address of ptr_three: %p", (void *) ptr_three); // HERE return 0; }
这些任务中的问题 –
ptr_one = &x; //ptr_one is an int * and &x is double *(assigning int * with double *) ptr_two = &x; //ptr_two is an int * and &x is double * ... ptr_three = &y; // similar reason
将ptr_one
和ptr_two
声明为双指针(double作为数据类型) –
double *ptr_one; double *ptr_two; double *ptr_three;
为什么math.h
会帮助你解决这个问题?