错误:C中为结构体省略了参数名称

时间:2014-05-01 02:08:06

标签: 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.)

struct student在头文件中声明:

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

和插入数组方法的.h中的原型:

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

3 个答案:

答案 0 :(得分:3)

这并非针对structs

在函数 prototypes (转发声明)中,您可以省略名称,例如

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

该函数的调用者存在转发声明。所以在这里,编译器实际上只需要知道期望的参数类型。考虑到原型(通常在头文件中找到)作为如何调用所述函数的文档,我强烈建议包含名称。否则,你可能会被整个大厅的开发者砸伤!

您的混淆可能来自语法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) {

}

答案 1 :(得分:1)

函数原型不需要为变量命名。以下两个是等效的:

void f(int x, struct my_struct y);
void f(int, struct my_struct);

但是,函数声明必须具有参数名称。因此,只有以下内容有效:

void f(int x, struct my_struct y) { .. }

在您的情况下,您只需在函数声明中添加student之后的struct student

答案 2 :(得分:-1)

尝试更改

void insert_array(struct student);

void insert_array(struct student student);

您的代码应该有效。