Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Old-style parameter declarations" error

Tags:

c

I'm having two compile errors when trying to compile my code, and I can't find what the issue really is. Could anybody help shed some light?

error: old-style parameter declarations in prototyped function definition
error: 'i' undeclared (first use in this function)

Code:

void printRecords (STUREC records[], int count)

STUREC records[ARRAY_MAX];
int count;
int i;
{
    printf("+---------------------------+--------+--------+--------+--------+--------+--------+---------+-------+\n");
    printf("| Student Name | ID | Test 1 | Test 2 | Proj 1 | Proj 2 | Proj 3 | Average | Grade |\n");
    printf("+---------------------------+--------+--------+--------+--------+--------+--------+---------+-------+\n");

    for (i = 0; i < count; i++)
    {
        size_t j;
        printf ("|%s|%d|%d|%d|%d|%d|%d|%f|%c|", records[i].name, records[i].id, records[i].score1,
                 records[i].score2, records[i].score3, records[i].score4, records[i].score5,
                  records[i].ave, records[i].grade);
    }

    return;
}
like image 287
seanncurtis Avatar asked Apr 13 '16 18:04

seanncurtis


3 Answers

Not for this specific question, but if you happen to come across this error, check that you did not miss a semicolon at the end of your declaration, because that's what happened to me.

like image 165
zbz.lvlv Avatar answered Oct 18 '22 06:10

zbz.lvlv


If you want to use old style C parameter declarations, you need to do this:

void printRecords(records, count)
    STUREC records[ARRAY_MAX];
    int count;
{
    int i;
    // ... rest of the code ...
}

But this is not considered a good practice and can make your code harder to read. Some compilers have even stopped supporting this syntax.

The other comments/answers are saying that you re-declare (and therefore hide) your function parameters in the body of the function, but this is not what you want to do (otherwise you effectively lose the parameters being passed in).

If you define a function like this:

void fxn(int num) {
    int num;
    num = num;
}

What does num refer to: the parameter or the local variable?

Either do this:

void printRecords(records, count)
    STUREC records[ARRAY_MAX];
    int count;
{
    int i;
    // ... rest of the code ...
}

or do this:

void printRecords(STUREC records[], int count)
{
    int i;
    // ... rest of the code ...
}

But don't try to do both or a mixture of the two.

like image 33
callyalater Avatar answered Oct 18 '22 07:10

callyalater


You have

void printRecords (STUREC records[], int count)

STUREC records[ARRAY_MAX];
int count;
int i;
{

But I guess you want:

void printRecords (STUREC records[], int count)
{
    int i;

EDIT: Thanks to callyalater for noting the redeclaration of the parameters in the function...

like image 1
mame98 Avatar answered Oct 18 '22 06:10

mame98