0

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);
}
6
  • What is the following error? Commented May 28, 2014 at 16:29
  • 1
    void display_jock(athlete *p) { --> void display_jock(struct athlete *p) { and put prototype before main. Commented May 28, 2014 at 16:31
  • "%10c%10c%10d%10.1f \n" --> "%10s %10s%10d%10.1f \n" Commented May 28, 2014 at 16:34
  • i get the following error when i implement your fix athletes.c:17: warning: conflicting types for 'display_jock' athletes.c:13: note: previous implicit declaration of 'display_jock' was here Commented May 28, 2014 at 16:34
  • Move display_jock() before main(). Commented May 28, 2014 at 16:36

1 Answer 1

3

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.