gpt4 book ai didi

c - 错误: Parameter name omitted in C for structs

转载 作者:行者123 更新时间:2023-11-30 20:06:30 26 4
gpt4 key购买 nike

对于 C 编码的一些新内容,我尝试查看堆栈上有关省略名称的其他帖子,但似乎找不到任何基于 C 中结构参数的内容。此代码只是 .C 文件中的一个片段,它使用头文件 (AssignBst.h) 并在主文件中实现:

 /* Used to insert the struct student into an array position, in order
based upon the numerical ordering of their I.D's within the array.
The Array being an array of structs declared in the .h file.
*/
void insert_array(struct student) /* Line22, Error points to here for omitted*/
{

int total_mem_req = 0;
int i=1;
struct student temp;

if(i<= n_students)
{
if(students[i].ID==0)
{
students[i].name = (char *) malloc(sizeof(char)*strlen(student.name));
strcpy(students[i].name,student.name);
students[i].ID = student.ID;
/* copy binary tree*/
return;
}

if((students[i] < student.ID) && (students[i+1] > student.ID)) /* Currently at location before desired insertion point*/
{
insert_array(student);
total_mem_req = strlen(student.name);
malloc(total_mem_req + 17); /* Added 17 bytes to account for the integer */
temp=students[i+1];
students[i+1] = student;
student=temp;
}

if((students[i].ID < student.ID) && (students[i+1].ID < student.ID)) /*if the current value and the next value in the array are less than the ID stored in student then increment i*/
{
i++;
}

}
n_students++; /* Increments the total number of students in the array*/
return;

}

当我编译这个时,我收到错误:

AssignBst.c: In function ‘insert_array’:
AssignBst.c:22: error: parameter name omitted
AssignBst.c:32: error: ‘student’ undeclared (first use in this function)
AssignBst.c:32: error: (Each undeclared identifier is reported only once
AssignBst.c:32: error: for each function it appears in.)

结构体student在头文件中声明:

    struct student
{
int ID;
char *name;
node_ptr units;
};

以及 .h 中插入数组方法的原型(prototype):

void insert_array(struct student); /*For inserting ID and name into the array*/ 

最佳答案

这并非特定于结构

在函数原型(prototype)(前向声明)中,您可以省略名称,例如

int foo(int, char*, struct student);

函数的调用者存在前向声明。因此,在这里,编译器实际上只需要知道期望的参数类型。考虑到原型(prototype)(通常在头文件中找到)充当如何调用所述函数的文档,我强烈建议包含名称。否则,你很可能会被大厅对面的开发人员打到!

您的困惑可能来自语法struct Student。请记住,类型的名称在这里都是单词,struct Student。除非使用typedef,否则您不能以任何其他方式引用它。

<小时/>

但是,实际函数定义需要参数名称。否则,你将如何引用参数?!

int foo(int number, char* name, struct student record) {

}
<小时/>

我还应该指出,很少有人真正想要将struct(按值)传递给函数。更有可能的情况是您将指针传递给struct:

int foo(int number, char* name, struct student *record_ptr) {

}

关于c - 错误: Parameter name omitted in C for structs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23401053/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com