//使用给定的实参:学号,姓名,成绩初始化给出的stu;c语言 int initStu(Student &stu, char *no, char *name, float score)
时间: 2024-09-11 11:16:39 浏览: 76
在C语言中,你可以使用指针来初始化结构体变量,这里`Student`是一个包含学号、姓名和成绩的结构体。`initStu`函数接受一个指向`Student`类型的引用,并利用传入的实参来设置这个学生的属性。以下是该函数的一个例子:
```c
#include <stdio.h>
#include <string.h>
// 假设有一个名为`Student`的结构体定义,类似这样:
typedef struct {
int id; // 学号
char name[50]; // 姓名
float score; // 成绩
} Student;
void initStu(Student &stu, char *no, char *name, float score) {
// 将输入转换为整数和字符串
stu.id = atoi(no); // 使用atoi将字符数组转换为整数
strncpy(stu.name, name, sizeof(stu.name)); // 不包括结束符'\0'
stu.name[sizeof(stu.name) - 1] = '\0'; // 确保字符串结束符被添加
stu.score = score;
}
int main() {
char no[] = "001";
char name[] = "张三";
float score = 90.0;
Student stu;
initStu(stu, no, name, score);
printf("学号: %d, 姓名: %s, 成绩: %.2f\n", stu.id, stu.name, stu.score);
return 0;
}
阅读全文
相关推荐



















