参数有结构体的函数怎么声明
时间: 2024-04-08 17:35:03 浏览: 13
要声明一个参数为结构体的函数,首先需要定义这个结构体。然后在函数声明中使用该结构体作为参数类型。下面是一个示例:
```c
// 定义结构体
typedef struct {
int id;
char name[20];
} Student;
// 声明函数,参数为结构体类型
void printStudent(Student student);
int main() {
// 创建结构体变量
Student myStudent;
myStudent.id = 1;
strcpy(myStudent.name, "John");
// 调用函数,并传入结构体变量作为参数
printStudent(myStudent);
return 0;
}
// 实现函数
void printStudent(Student student) {
printf("ID: %d\n", student.id);
printf("Name: %s\n", student.name);
}
```
在上述示例中,我们定义了一个名为`Student`的结构体,并声明了一个名为`printStudent`的函数,该函数的参数类型为`Student`结构体。在`main`函数中,我们创建了一个`myStudent`变量,并将其作为参数传递给`printStudent`函数。在`printStudent`函数中,我们可以访问传入的结构体参数的成员并进行打印操作。