好的,我有一个名为 main.c
的主要源代码,一个名为 test.h
的头文件,以及另一个名为 handBeraknare.c
的类>。
我试图通过将我的一些方法转移到类 handBeraknare.c
来使我的代码更具可读性。
所以在 main.c
中我有一个看起来像这样的结构:
typedef struct kort{
int draget;
char farg;
int nummer;
struct kort *next;
}kort; `
在 main.c
中,我使用 kort k=(kort*)malloc(sizeof(kort));
创建了其中的几个,并将它们放入数组中。我想要实现的是将这个 kort
数组发送到 handBeraknare.c
中的一个函数,但是我在 handBeraknare 包含的文件中得到了一些奇怪的错误 ".c”
。
我猜测这与头文件有关,现在知道 "kort"
是什么(我的结构)。不管怎样,这里有一些代码:
// in test.h
int beraknaFarg(kort kortHand[]);
// in handBeraknare.c
#include <stdio.h>
#include "test.h"
int beraknaFarg(kort kortHand[]){
char c = kortHand[0].farg;
int i;
for (i=1;i<5;i++){
if (kortHand[i].farg!=c){
printf("inte färg");
system("pause");
//Spelaren har inte färg. Retunera 0
return 0;
}
}
//Spelaren har färg. Retunera 1
printf("!!!!färg");
system("pause");
return 1;
}
//part of the main class. Calling function test()
// which calls the method beraknaHand which exists in handBeraknare.c
#include "test.h"
...
int main(int argc, char *argv[])
{
test();
}
// the testfunction in my mainclass
void test(){
char farg[4]={'S','K','R','J'};
int nummer[14]={0,2,3,4,5,6,7,8,9,10,11,12,13,14};
kort kortArray[52];
kort kortHand[5];
kort *k;
k=(kort*)malloc(sizeof(kort));
k->farg='s';
k->nummer=5;
kortHand[0]=*k;
k->farg='s';
k->nummer=11;
kortHand[1]=*k;
k->farg='s';
k->nummer=12;
kortHand[2]=*k;
k->farg='s';
k->nummer=11;
kortHand[3]=*k;
k->farg='s';
k->nummer=9;
kortHand[4]=*k;
beraknaFarg(kortHand);
最佳答案
让test.h读取
typedef struct kort{
int draget;
char farg;
int nummer;
struct kort *next;
} kort;
int beraknaFarg(kort kortHand[]);
并从 main.c 中删除 typedef
关于c - 创建头文件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9530451/