user3363135
user3363135

Reputation: 370

displaying values in c structure array

I'm trying to display the values of my structure array, the compiler throws out the following error:

athletes.c:17: error: expected ')' before '*' token

Could someone help me out with a solution and if possible an explanation as to what I did wrong.

#include <stdio.h>
#include <string.h>
struct athlete {
  char last_name[25];
  char first_name [20];
  int rank;
  float salary;
  };


int main (void) {
    struct athlete players[] = {{"Lebron", "James",1,25}, {"Durant", "Kevin",3,20},{"Duncan","Tim",2,12}};
    display_jock(players);

}

void display_jock(athlete *p) {
    printf ("%10c%10c%10d%10.1f \n", p[0].last_name,p[0].first_name,p[0].rank, p[0].salary);
    printf ("%10c%10c%10d%10.1f \n", p[1].last_name,p[1].first_name,p[1].rank, p[1].salary);
    printf ("%10c%10c%10d%10.1f \n", p[2].last_name,p[2].first_name,p[2].rank, p[2].salary);
}

Upvotes: 0

Views: 103

Answers (1)

Chris Maes
Chris Maes

Reputation: 37772

there are several small errors:

  • your code does not know the type "athlete", so your class display_jock should be defined with argument struct athlete: void display_jock(struct athlete *p)
  • you should forward declare that function: otherwise main doesn't know it (edit:you could also just move the display_jock function at the top of the main function)
  • when printing a char array, you should use %s instead of %c when using printf.
  • your main function should return an int (since it is declared as int main...)

this is your code fixed up:

#include <stdio.h>
#include <string.h>
struct athlete {
  char last_name[25];
  char first_name [20];
  int rank;
  float salary;
  };

void display_jock(struct athlete *p);


int main (void) {
    struct athlete players[] = {{"Lebron", "James",1,25}, {"Durant", "Kevin",3,20},{"Duncan","Tim",2,12}};
    display_jock(players);
    return 0;

}

void display_jock(struct athlete *p) {
    printf ("%s%s%10d%10.1f \n", p[0].last_name,p[0].first_name,p[0].rank, p[0].salary);
    printf ("%s%s%10d%10.1f \n", p[1].last_name,p[1].first_name,p[1].rank, p[1].salary);
    printf ("%s%s%10d%10.1f \n", p[2].last_name,p[2].first_name,p[2].rank, p[2].salary);
}

Upvotes: 3

Related Questions