10

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;
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
champlooSIXSIX
  • 131
  • 1
  • 1
  • 9
  • 2
    looks like you misplaced the `{` bracket at the printRecords function definition .. – jboockmann Apr 13 '16 at 19:03
  • The opening `{` should follow the function parameter list. – Eugene Sh. Apr 13 '16 at 19:04
  • ah! thank you guys... now i'm getting an error saying that in my function 'printRecords' 'count' is redeclared as a different kind of sybole with a note stating where my previous definition is. but i have `void printRecord (STUREC records[], int count)` followed by a local declaration of `int count;`... those are the two lines the error code is point at, but i have them both at int, so what could the error be referring to? – champlooSIXSIX Apr 13 '16 at 19:15

3 Answers3

19

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.

zbz.lvlv
  • 3,597
  • 6
  • 34
  • 38
3

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.

Community
  • 1
  • 1
callyalater
  • 3,102
  • 8
  • 20
  • 27
  • yeah, i just moved my { up haha and it took care of it. im now having an error saying my count in printRecords is being redeclared as a different symbol... the error points to lines 98 and 94. any clue? – champlooSIXSIX Apr 13 '16 at 19:24
  • @seanncurtis Get rid of the lines immediately after the `{` but before `int i;`. See the last example in my answer. – callyalater Apr 13 '16 at 19:26
  • oh i see, if you declare count is an int in the () starting the function, you don't need to declare that in local declarations? – champlooSIXSIX Apr 13 '16 at 19:31
  • Bingo! Otherwise, the function doesn't know which one you are talking about. – callyalater Apr 13 '16 at 19:31
  • makes perfect sense, thank you. i am now receiving an error i've never seen in the 4 months i've been learning c. it says 'error: 1d returned exit status'... it doesnt refer to a line or anything though. – champlooSIXSIX Apr 13 '16 at 19:33
  • That is a linker error and is completely different than your question here. There are a lot of answers about `ld returned exit status 1` on SO and online. – callyalater Apr 13 '16 at 19:35
  • @seanncurtis If you think my answer helped, please upvote so that future users can apply this if they are encountering the same problem. – callyalater Apr 13 '16 at 19:37
  • @seanncurtis [This question](http://stackoverflow.com/questions/27593029/c-compile-collect2-error-ld-returned-1-exit-status) explains more about linker errors. – callyalater Apr 13 '16 at 19:40
  • @callyalater What compilers have stopped supporting this syntax? – Xatenev Nov 26 '18 at 23:20
1

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

mame98
  • 1,271
  • 12
  • 26
  • Also, `i` is not a parameter in `printRecords` and the old style function parameters mean that you do not put the type in the function parameter declarations. – callyalater Apr 13 '16 at 19:12
  • ah! thank you guys... now i'm getting an error saying that in my function 'printRecords' 'count' is redeclared as a different kind of sybole with a note stating where my previous definition is. but i have `void printRecord (STUREC records[], int count)` followed by a local declaration of `int count;`... those are the two lines the error code is point at, but i have them both at int, so what could the error be referring to? – champlooSIXSIX Apr 13 '16 at 19:16
  • @mame98 Your example doesn't do what you think it does. You re-declare the parameters as local variables which *hides* the parameters and makes them inaccessible for the rest of the function. – callyalater Apr 13 '16 at 19:31