c - 打印结构中的项目

标签 c

我想打印 c 中结构中的元素,但发送和第三个打印语句给我警告:格式指定类型“char *”,但参数具有类型“char”。我知道这与指针有关,但我不知道我做错了什么。我还对其进行了修改以显示我正在使用的 2 个结构。

struct student_record{
int student_id;
int student_age;
char first_name;
char last_name; };


struct student_record_node{
struct student_record* record;
struct student_record_node* next;
struct student_record_node* prev; };


void printNode(struct student_record_node *node){
printf("Struct student_record_node: \n");
printf("        student first_name: %s\n", node->record->first_name);
printf("        student last_name: %s\n", node->record->last_name);
printf("        student id: %d\n", node->record->student_id);
printf("        student age: %d\n", node->record->student_age);
printf("\n");}

最佳答案

在student_record的结构声明中

字符名字; 字符姓氏;

表明first_name和last_name是两个字符,而不是字符数组(即字符串)

当使用 printf("%s", ELEMENT) 时,%s 需要字符数组的内存地址,即。 pointer(char *) 但由于您传递了一个字符,因此会导致语法错误。

要修复代码,请编辑结构声明,使其成为固定长度的静态数组,或动态地将内存分配给函数中的字符指针。

关于c - 打印结构中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43600271/

相关文章:

c - 在自己的 c shell 中实现历史命令并重新运行旧命令

javascript - 为什么 C# 和 ECMAScript ISO 标准可以免费获得,而 C/C++ 却不能?

无法删除C中的文件

C - 从命令行参数中获取 fread 参数

c - 可能的模式错误

c++ - C/C++ - 共享内存中的环形缓冲区(POSIX 兼容)

c - 如何多次使用 strchr() 查找第 n 次出现

c - 将简单的字符标记放置在阵列板上以实现简单的糖果粉碎游戏

c++ - RapidJSON 库通过索引获取数组中的值

c - 如何在C中使用printf显示int