嘿,我是一个新手程序员(通过C语言的cs50x学习),当他们提到结构体时,我决定尝试一下并编写一个快速程序,使用函数交换结构体中的一些值。但是我遇到了几个错误消息,其中第一个是“不兼容的指针类型,将'struct numbers *'传递给'type struct numbers *'的参数。另一个问题似乎出现在函数定义中,编译器说“类型为'struct number'的不完整定义”。我希望得到一些帮助,因为我感到困惑。
这是代码(我知道它很粗糙,但我正在学习lol)
#include <stdio.h>
struct numbers;
void swap(struct numbers* s);
int main(void)
{
struct numbers
{
int a;
int b;
int c;
};
struct numbers x = {1, 5 , 9};
swap(&x);
printf("%i, %i, %i\n", x.a, x.b, x.c);
return 0;
}
void swap(struct numbers* s)
{
int temp = s -> a;
int temp2 = s -> b;
s -> a = s -> c;
s -> b = temp;
s -> c = temp2;
}
main
之外。 - user253751