admin 管理员组

文章数量: 1087139


2024年5月21日发(作者:并非所有变量都已绑定 什么原因算sql注入吗)

c语言外部声明结构体

如何在C语言中进行结构体的外部声明?

在C语言中,可以使用关键字“extern”来声明结构体类型,以使得在其他文

件中也能够使用该结构体类型。

例如,下面的代码示例中,定义了一个名为“Student”的结构体类型,并在文

件“student.h”中进行了声明:

student.h

struct Student {

char name[20];

int age;

float score;

};

extern struct Student stu; 外部声明结构体类型,可供其他文件使用

然后,在其他文件中,可以通过引入“student.h”头文件,并使用“stu”变量

来操作该结构体类型:

otherfile.c

#include "student.h"

#include

void printStudent() {

printf("name: %sn", );

printf("age: %dn", );

printf("score: %.2fn", );

}

需要注意的是,外部声明的结构体类型必须在该类型的定义之前进行。如果在被

引用的文件中找不到对应的结构体类型定义,会导致编译错误。


本文标签: 结构 类型 使用 声明