警告:从不兼容的指针类型分配[默认启用]

时间:2015-09-19 08:52:24

标签: c

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

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可能会解决这个问题,但却是否定的。请修理它需要帮助

3 个答案:

答案 0 :(得分:4)

下面:

ptr_one = &x;
ptr_two = &x;
ptr_three = &y;

ptr_oneptr_twoptr_threeint*&x&ydouble*。您正尝试为int*分配double*。因此警告。

通过将指针类型更改为double*而不是int*来修复它,即更改以下行

int *ptr_one;
int *ptr_two;
int *ptr_three;

double *ptr_one;
double *ptr_two;
double *ptr_three;

答案 1 :(得分: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 ans ptr_two声明为双指针(double作为数据类型) -

double *ptr_one;
double *ptr_two;
double *ptr_three;

为什么math.h会帮助你解决这个问题?

答案 2 :(得分:0)

以下更改应该

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

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;
}