Reputation: 103
I am new to C language. I'm currently trying to create an algorithm to make a rank of scores, following a textbook. This is a code shown in the textbook.
void rank1 (struct data *x, int n)
{
int i, j;
for ( i = 0 ; i < n; i++ )
x [i]. rank = 1;
for (i=0;i<n;i++)
for (j=0;j<n;j++)
if(x [i].score < x [j].score)
x [i].rank++;
}
Then I've got error saying below
tempCodeRunnerFile.c:1:20: warning: 'struct data' declared inside parameter list will not be visible outside of this definition or declaration
1 | void rank1 (struct data *x, int n)
| ^~~~
tempCodeRunnerFile.c: In function 'rank1':
tempCodeRunnerFile.c:6:11: error: invalid use of undefined type 'struct data'
6 | x [i]. rank = 1;
| ^
tempCodeRunnerFile.c:6:11: error: dereferencing pointer to incomplete type 'struct data'
tempCodeRunnerFile.c:9:10: error: invalid use of undefined type 'struct data'
9 | if(x [i].score < x [j].score)
| ^
tempCodeRunnerFile.c:9:24: error: invalid use of undefined type 'struct data'
9 | if(x [i].score < x [j].score)
| ^
tempCodeRunnerFile.c:10:7: error: invalid use of undefined type 'struct data'
10 | x [i].rank++;
| ^
What can I do for this situation?
Upvotes: 0
Views: 1375
Reputation: 151
Your struct has not been defined declared. When the compiler sees this function, it does not know what struct data
means. Somewhere before this function, define declare that struct.
Something like this:
struct data{
int rank;
...
};
<Your function here>
Upvotes: 1