JavaScript is required

Đoạn mã sau kết quả là gì?

struct Employee {

char Code[], name[]

long Salary;

};

Employee e1 = { "E089", "Hoang so", 12000}, e2 = e1;

printf("%ld", e1.Salary + e2->Salary);

A.

24000.

B.

12000.

C.

Đoạn mã bị lỗi.

D.
Kết quả khác.
Trả lời:

Đáp án đúng: C


The C code has several syntax errors: 1. **Array declaration `char Code[], name[]` in struct:** When declaring a character array (string) in a struct, you need to specify the size of the array, e.g., `char Code[10]; char name[50];`. If the size is not specified, the compiler will not know how much memory needs to be allocated for this array. 2. **Assigning values to a struct:** Assigning `e2 = e1;` will copy the members of `e1` to `e2`. However, due to the incorrect declaration of the `Code` and `name` arrays (missing size), the result will not be as expected and may cause errors. 3. **`->` operator:** The `->` operator is used to access the members of a struct through a pointer to that struct. In the code, `e2` is a direct struct variable (not a pointer), so you should use the `.` (dot) operator to access the `Salary` member of `e2`, i.e., `e2.Salary`. Because there are many syntax errors in the code, the compiler will report errors and the program will not be able to compile or run. Therefore, the correct answer is "The code contains errors".

Câu hỏi liên quan