Question

Some what new to coding in C, I have tried looking at other posts on stack about omitted names but cannot seem to find any based around struct parameters in C. This code is just a fragment from the .C file, which uses a header file (AssignBst.h) and is implemented in a main file:

 /* 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;

}

When I compile this, I get the errors:

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.)

The struct student is declared in the header file:

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

and the prototype in the .h for the insert array method:

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

Solution

This isn't specific to structs.

In function prototypes (forward declarations), you are allowed to omit the name, e.g.

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

A forward declaration exists for callers of the function. So here, the compiler really only needs to know what type of parameters to expect. Considering that the prototype (often found in a header file) serves as documentation on how to call said function, I strongly recommend that you include the names. Otherwise, you're likely to get smacked by the dev across the hall!

Your confusion may come from the syntax struct student. Keep in mind that the type's name is both words here, struct student. You can't refer to it in any other way, unless you use a typedef.


However, the parameter name is required for the actual function definition. Otherwise, how would you refer to the parameter?!

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

}

I should also point out that it is rare that one actually wants to pass a struct (by value) to a function. More likely is the case that you pass a pointer to a struct:

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

}

OTHER TIPS

A function prototype doesn't need to give names for variables. The following two are equivalent:

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

However, a function declaration must have name for arguments. Thus, only the following is valid:

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

In your case, you just have to add one more student after struct student in the function declaration.

Try to change

void insert_array(struct student);

to

void insert_array(struct student student);

Your code should work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top