C语言 警告:来自不兼容指针类型的赋值[默认情况下启用]

waxmsbnn  于 2023-04-05  发布在  其他
关注(0)|答案(4)|浏览(183)
#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可能会修复它,但否定的。请需要帮助修复它

nr7wwzry

nr7wwzry1#

此处:

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

ptr_oneptr_twoptr_threeint* s,&x&ydouble* s。您试图将int*double*分配。因此出现警告。
通过将指针的类型更改为double*而不是int*来修复它,即更改以下行

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

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

eeq64g8w2#

下面的变化应该做

#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;
}
8wtpewkr

8wtpewkr3#

作业中的问题-

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会在这个问题上帮助你?

vql8enpb

vql8enpb4#

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

相关问题