gpt4 book ai didi

c - 在另一个源文件中的函数中使用结构元素

转载 作者:行者123 更新时间:2023-12-04 01:04:56 24 4
gpt4 key购买 nike

刚开始学习 c,我在使用第一个源文件中的结构变量的另一个源文件中编写函数时遇到了困难。

基本上我需要在另一个文件中创建一个 bubblesort 函数以在我的驱动程序中使用

这就是我想要做的

驱动.c

#include<stdio.h>
#include<stdlib.h>
#include<sort.c>
typedef struct iorb {
int base_pri;
struct iorb *link;
char filler[100];
} IORB;

int main(){
sort();
}

排序.h

void sortList(IORB * head, int(*prio)(int));

排序.c

void sortList(IORB * head, int(*prio)(int)){
// do stuff
}

但是我得到这个错误:排序函数中的未知类型 IORB。

如何将 IORB 传递给函数?

尝试答案后更新代码:driver.c

#include<stdio.h>
#include<stdlib.h>
#include "sort.h"
#include "iorb.h"
//removed the typedef in the driver

iorb.h

#ifndef IORB_H
#define IORB_H

typedef struct iorb {
int base_pri;
struct iorb *link;
char filler[100];
} IORB;

#endif

排序.h

#ifndef SORT_H_
#define SORT_H_


void sortList(IORB * head, int(*prio)(int));

#endif

排序.c

#include "sort.h"
#include<stdlib.h>
#include<stdio.h>
#include "iorb.h"


void sortList(IORB * head, int(*prio)(int)){
//do stuff
}

最佳答案

您想将类型声明/typedef 放在头文件中:

#ifndef IORB_H
#define IORB_H

typedef struct iorb {
int base_pri;
struct iorb *link;
char filler[100];
} IORB;

#endif

然后在 sort.h 中包含该头文件:

#include "iorb.h"

并且您想在 sort.c 中包含 sort.h:

#include "sort.h"

关于c - 在另一个源文件中的函数中使用结构元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66864422/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com