[英]How can i dereference a pointer to struct in C?
我在最后一个printf语句中收到错误:一元'*'(have'int')的无效类型参数。 我想*不会取消引用变量“ res”,因为它不是指向整数的指针。 我不知道如何解决这个问题。 我非常感谢您的帮助。 提前致谢。
//program to find the pair of numbers that summed have has a result a given
//number n
//ex. input: 8; output: (1,7) (2,6) (3,5) (4,4)
//input: 11; output: (1,10) (2,9) (3,8) (4,7) (5,6)
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
int x;
int y;
} couple;
couple* sums(int n)
{
int i;
couple *c = (couple*) malloc(sizeof(couple)*n/2);
for ( i=0; i<n/2; i++)
{
c[i].x = i+1;
c[i].y = n-i-1;
}
return c;
}
int main()
{
int n,i;
couple *res;
scanf("%d",&n);
res=sums(n);
for(i=0;i=n/2;i++)
{
printf("(%d,%d)",*(res[i].x),*(res[i].y));
}
return 0;
}
`
您拥有的是int
,因此无需使用*
取消引用。
另外,您的循环条件不正确。 您正在做一个需要比较的作业:
for(i=0;i<n/2;i++)
{
printf("(%d,%d)", res[i].x, res[i].y);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.