Thread: structs...annoying compilation problem

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    structs...annoying compilation problem

    I can't seem to get the following to compile:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void fillEmployee(employee emp, char *name, double rate, int num);
    void swap(employee employees[], int a, int b);
    void sortEmployees(employee employees[], int number);
    void printEmployees(employee employees[], int number);
    
    struct employeeRecord
    {
       char surname[20];
       double hourlyRate;
       int empNumber;
    }
    typedef struct employeeRecord employee;
    
    int main()
    {
            employee employees[5];
    
    }

    5: error: syntax error before "emp"
    6: error: syntax error before "employees"
    7: error: syntax error before "employees"
    8: error: syntax error before "employees"


    why is it not accepting employee employees[] as the parameter for the prototype methods

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    The type 'employee' has not been defined, and the compiler reads code from front to end. Put your struct description and typedef before your function declarations. And I think you want a semicolon after your struct description.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    32
    You need to put the struct def above the functions that use it. Also you can simplify your struct defintion and typedef into a single statement

    Code:
    typedef struct employeeRecord
    {
       char surname[20];
       double hourlyRate;
       int empNumber;
    }employee;
    Doesn't help any preformance wise, but just cleans up the code a bit.

  4. #4
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    oh i see thanks.

    Hmm another problem has come up:

    Code:
    void fillEmployee(employee emp, char *name, double rate, int num)
    {
       strcpy(emp->surname, name);
      emp->hourlyRate = rate;
      emp->empNumber = num;
    }

    37: error: invalid type argument of `->'
    38: error: invalid type argument of `->'
    39: error: invalid type argument of `->'


    even emp->hourlyRate = 18; doesn't seem to work.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The -> operator is used with a pointer to a struct or union: emp is neither.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    The -> operator is used for memory addresses. Dot is used for structures themselves. In the following example, we let x be an employee structure, and we have y be a memory address of an employee structure.

    Code:
    employee x;
    employee *y; /* y can hold a memory address of an employee. */
    int u,v;
    x.empNumber = 7;
    u = x.empNumber; /* assigns 7, the value in x.empNumber, to u */
    
    y = &x; /* Assign to y the memory address of x. */
    u = (*y).empNumber; /* Dereferences y, gets the empNumber member. */
    
    if (u == v) puts("They're both seven!");
    
    u = y->empNumber; /* Dereferences y, gets the empNumber member (like before). */
    if (u == v) puts("Again!");
    
    y->empNumber = 3; /* same as (*y).empNumber = 3 */
    if (x.empNumber == 3) puts ("Three!");
    In C, ((*a).b) means the same thing as (a->b)

  7. #7
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    thanks.

    Hmm i can't seem to print out the values inside the struct. It just prints out long numbers for some reason:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    typedef struct employeeRecord employee;
    
    struct employeeRecord
    {
       char surname[20];
       double hourlyRate;
       int empNumber;
    };
    
    void fillEmployee(employee emp, char *name, double rate, int num);
    void swap(employee employees[], int a, int b);
    void sortEmployees(employee employees[], int number);
    void printEmployees(employee employees[], int number);
    
    int main()
    {
       employee employees[5];
    
       fillEmployee(employees[0], "Sandra", 35.75, 053);
        printEmployees(employees, 5);
      return 0;
    }
    
    
    
    void fillEmployee(employee emp, char *name, double rate, int num)
    {
      strcpy(emp.surname, name);
      emp.hourlyRate = rate;
      emp.empNumber = num;
    
    }
    
    void printEmployees(employee employees[], int number)
    {
       int i;
    
       for (i=0; i<number; i++)
       {
       //  printf("%-20s%4d   %.2f\n", employees[i].surname,
         //    employees[i].empNumber, employees[i].hourlyRate);
           printf("\n%d", employees[i].empNumber);
       }
       printf("\n");
    }
    gives me the following:


    0
    0
    -1073743504
    134513333
    1311707124


  8. #8
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    You need to change fillEmployee to accept a pointer to an employee. Otherwise, you're modifying the values of a copy, not the original.

    Also, even if you filled your first employee successfully, you'd still be printing out the contents of four other noninitialised employees.

    By the way, 053 is not fifty-three.

  9. #9
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Quote Originally Posted by Rashakil Fol
    You need to change fillEmployee to accept a pointer to an employee.
    hmm where do i get that pointer from?

    is it possibly like this?

    fillEmployee(*employees, "Cogswell", 35.75, 43);

    i tried it and it made no difference.



    Also, even if you filled your first employee successfully, you'd still be printing out the contents of four other noninitialised employees.
    yup, i pull those out on purpose to minimize the code.


    fillEmployee(employees[1], "Alroy", 12.00, 163);
    fillEmployee(employees[2], "Jetson", 15.75, 97);
    fillEmployee(employees[3], "Astro", 10.25, 104);
    fillEmployee(employees[4], "Sprocket", 22.75, 15);

    [/QUOTE]

    By the way, 053 is not fifty-three.
    fixed

  10. #10
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Ok, i don't know if this is the ideal way of doing it but what i've done is:

    modified the fillEmployee method, i've included an index parameter (int). In the main method i access the fill method and pass along the index number, name, hourly rate etc.

    The fillEmployee method then uses this to fill the struct:

    employees[number].hourlyRate = rate;
    so if i pass the index 2 then the 2nd struct name will be filled etc.
    ...etc

    which is then printed.

    I'm intrested to know the pointer variation to the solution :-)

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    struct employees, *pemp;
    pemp = &employees[number];
    
    // employees[number].hourlyRate == pemp->hourlyRate by now
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I think, I don't know, that
    Code:
    typedef struct employeeRecord employee;
    
    struct employeeRecord
    {
       char surname[20];
       double hourlyRate;
       int empNumber;
    };
    Should be
    Code:
    struct employeeRecord
    {
       char surname[20];
       double hourlyRate;
       int empNumber;
    };
    
    typedef struct employeeRecord employee;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    It doesn't need to be. Some prefer doing it the former way.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with pointers to structs
    By mike_g in forum C Programming
    Replies: 2
    Last Post: 03-23-2008, 12:03 PM
  2. Problem with passing structs to a function.
    By darsh1120 in forum C Programming
    Replies: 7
    Last Post: 03-11-2008, 04:36 AM
  3. Array of structs Problem
    By I BLcK I in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2008, 01:20 AM
  4. annoying problem, solution should be simple...
    By Captain Penguin in forum C++ Programming
    Replies: 0
    Last Post: 10-18-2002, 04:41 PM