我想打印 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/