-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.

9
  • 2
    struct record[0] declares an array of size 0. Commented Oct 24, 2017 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}; Commented Oct 24, 2017 at 10:48
  • I dun understand. Please explain more. Why need to cast (struct Record)? Commented Oct 24, 2017 at 10:49
  • @TeoPeiShen don't listen the solution of BLUEPIXY, it's correct but that will confuse you in this state. Commented Oct 24, 2017 at 10:50
  • 1
    Please review 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. Commented Oct 24, 2017 at 10:51

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

8 Comments

So, the struct array is not like int array or char array that starts from [0]?Thanks for fast response.
struct record[1] --> struct Record record[1] (And use another name for variable name of the array)
@Bluepix, thanks. fixed that.
Moreover, it seems that the type of employeeName is wrong.
#include <stdio.h> int main (){ struct Record{ int employeeNumber; char employeeName; float salary; int yearsServiced; } record[5]; struct record[1] ={46723,"Fattah",4550.00,8}; printf("TheEmployee number is %d",record[1].employeeNumber); }
|
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.

Comments

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);

}

1 Comment

I understand your solution but do you have way i can construct all the elements in one line. Thanks for your fast response.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.