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

标签 c arrays struct

对于 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/

相关文章:

c - 错误: invalid type argument of '->' (have 'Personaggio' )

c - 如何判断每个随机数是否命中错误

php - 计算并添加数组 php 中的数组值

python - 迭代多维数组并跟踪/执行迭代索引操作的最佳方法是什么

c - 用 void * 初始化 C 中的数组

c# - 使用结构而不是给构造函数 8 个参数是否正确?

c - 构建一个读取数组的c程序

c - 是否可以在文本文件中输出格式化文本?

c - 字数统计程序不起作用。代码示例来自《The C-programming Language》一书 Ritchie & Kernighan

php - 如何通过在 PHP 中使用键增加数组计数来附加数组元素?