如何在 C 中取消引用指向 struct 的指针?

山姆_mb_98

我在最后一个 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;
}

`

灌木丛

您拥有的是ints,因此无需使用*取消引用。

此外,您的循环条件不正确。你正在做一个你想要比较的任务:

for(i=0;i<n/2;i++)
{
    printf("(%d,%d)", res[i].x, res[i].y);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章