-4
#include <stdio.h>
int main () {
    struct Record {
        int employeeNumber;
        char employeeName;
        float salary;
        int yearsServiced;  
    } record[5];

    struct record[0] = {46723, "Fattah", 4550.00, 8};
    printf("TheEmployee number is %d", record[0].employeeNumber);

}

Why my program cannot run? please help. Thanks for advance.

ilim
  • 4,477
  • 7
  • 27
  • 46
TeoPeiShen
  • 43
  • 1
  • 7
  • 2
    `struct record[0]` declares an array of size 0. – Paul Ogilvie Oct 24 '17 at 10:47
  • 1
    `char employeeName;` --> `char *employeeName;`, `struct record[0] = {46723, "Fattah", 4550.00, 8};` --> `record[0] = (struct Record){46723, "Fattah", 4550.00, 8};` – BLUEPIXY Oct 24 '17 at 10:48
  • I dun understand. Please explain more. Why need to cast (struct Record)? – TeoPeiShen Oct 24 '17 at 10:49
  • @TeoPeiShen don't listen the solution of BLUEPIXY, it's correct but that will confuse you in this state. – Stargateur Oct 24 '17 at 10:50
  • 1
    Please review https://stackoverflow.com/help/how-to-ask ... your question whilst it may be answered does not help anyone experiencing similar issues in the future and adds nothing to StackOverflow. – David Oct 24 '17 at 10:51
  • @TeoPeiShen _Why need to cast (struct Record)?_ It means compound literals, not casts. – BLUEPIXY Oct 24 '17 at 10:56
  • Also, you can implement with array initialization list. E.g `} record[5];` --> `} record[5] = { {46723, "Fattah", 4550.00, 8} };` – BLUEPIXY Oct 24 '17 at 10:58
  • You can not use an initialization list as an assignment expression to an array element. For the sake of convenience, you can use a compound literal to make a similar writing as shown above. However, it can not be used in older versions of C. You need to use the initialization list at declaration or use an assignment expression for each member. – BLUEPIXY Oct 24 '17 at 11:13
  • Thanks BlUEPIXY. the casting way works because I got 5 more struct to key in. record[0]=(struc Record){1234,"Fattah",7845.98,8} works from record[0] to record[4]. – TeoPeiShen Oct 25 '17 at 03:08

3 Answers3

1

struct record[0] declares an array of size 0. You intend to initialize the first element of the array and you confuse declaring and indexing:

struct Record myRecord[1] = {46723, "Fattah", 4550.00, 8};

This declares an array of size 1 and initializes the first element with the given values.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
0

You declared employeeName as char but you pass in a string. employeeName needs to be a pointer on char.

char *employeeName;

#include <stdio.h>
int main () {
    struct Record {
        int employeeNumber;
        char *employeeName;
        float salary;
        int yearsServiced;  
    } record[5];

record[0].employeeNumber = 46723;
    record[0].employeeName = "Fattah";
    record[0].salary = 4550.00;
    record[0].yearsServiced = 8;

    printf("TheEmployee number is %d", record[0].employeeNumber);

}
Fachher
  • 28
  • 4
  • I understand your solution but do you have way i can construct all the elements in one line. Thanks for your fast response. – TeoPeiShen Oct 24 '17 at 11:19
0

Firstly, by struct record[0] you are declaring an array of size 0, which you need to change to struct record[1].

And as far as your doubt regarding casting, you should read Struct initialization of the C/C++ programming language?

Lastly , I don't think it is the reason of your error but why are you trying to assign "Fattah" to a variable of type char.

Aditi Rawat
  • 784
  • 1
  • 12
  • 15