2

I read a tutorial in which there is this struct:

struct
{
    char Name[25];
    int Age;
    float SkillRating;
} Employee;

defines a new aggregate, called Employee, containing fields called Name (of type character), Age (of type integer), and SkillRating (of type float).

In contrast, the C statement:

struct EmployeeType
{
    char Name[25];
    int Age;
    float SkillRating; 
};

does not define a new aggregate variable, but defines a new aggregate type, EmployeeType. This new data type could then be used to declare variables in the same way as a primitive data type. That is, in the same way that C allows the variable x to be declared as an integer using the statement

I am confused here. Does the distinction exist if place 'Emplyee` on different position?

I guess they are identical.

Blaze
  • 16,736
  • 2
  • 25
  • 44
Alice
  • 1,360
  • 2
  • 13
  • 28
  • 2
    "Does the distinction exist if place 'Emplyee` on different position?" yes. And you can also place both EmployeeType and Employee in a single statement which will simply combine the two meanings. – Support Ukraine Mar 07 '19 at 07:16
  • 1
    The `struct` syntax is yet another language failure in C. It's simply inconsistent and opens up for different styles. I would advise to do this: if you need a struct type, use `typedef struct {...} type;` If you need a struct variable, use `type name;`. In the rare occasion where you need a self-referencing struct (like a linked list node), do this: `typedef struct tag { ... struct tag* next; } type;`. And that's it. – Lundin Mar 07 '19 at 08:02
  • 1
    @Alice. I had the same problem in the past and the worst is yet to come when typedef will be used with struct. You will see syntax like `typedef struct X {...} X; or typedef struct {...} X, typedef struct X X, ...`. So, I suggest you to read these posts https://stackoverflow.com/q/1675351/7462275 and https://stackoverflow.com/q/252780/7462275 – Stef1611 Mar 07 '19 at 08:14

3 Answers3

4

In the first case, the struct is unnamed and Employee is a variable of that unnamed struct. You can directly modify it like this:

int main()
{
    Employee.Age = 100;
    return 0;
}

In the second case, EmployeeType is just a type, but you didn't make any instance of it yet. You can make any amount of instances:

int main()
{
    struct EmployeeType a;  // employee on the stack
    a.Age = 100;
    struct EmployeeType *b = malloc(sizeof(struct EmployeeType)); // employee on the heap
    if (b) { // set the age of b if the allocation succeeded
        b->Age = 100;
    }
    free(b); // malloc-allocated memory must be freed afterwards 
    return 0;
}

You can even do both at once:

struct EmployeeType
{
    char Name[25];
    int
        Age;
    float SkillRating;
} Employee;

Here, Employee is one instance of it, but you can make additional instances:

int main()
{
    Employee.Age = 100;
    struct EmployeeType a;  // employee on the stack
    a.Age = 100;
    return 0;
}
Blaze
  • 16,736
  • 2
  • 25
  • 44
3
struct A_s { int memb; }; // declares a struct A_s
struct A_s a; // declares a variable a of type struct A_s

now you can combine struct declaration with variable declaration:

// declares struct A_s and in the same line declares variable a
struct A_s { int memb; } a; 

Now you can create an anonymous struct by omitting the structure tag name:

// declares anonymous struct and in the same line declares variable a
struct { int memb; } a;

Now structure declaration can be really put anywhere:

int func(int a, struct A_s { int memb; } b);
struct A_s { int memb; } func(int a);
struct B_s {
    int memb1;
    struct A_s {
         int memb2;
    } a;
};

I think the description of the C code of the field "name" in your post is invalid. The field name inside "aggregate" (read as: structure) has the type "array of 25 characters", not character type.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
1

struct keyword in C can be used to declare aggregate datatypes, aggregate data objects or both.

In your first declaration (and definition), a tag name is missing, so an anonymous object 'Employee' is created. 'Employee' remains the only object of this struct. You cannot create more objects from this struct anywhere else in the code.

In your second declaration, you have created a struct type which can be instantiated many times (i.e., multiple instances of this struct can exist) as shown below -

struct EmployeeType employee_1;
struct EmployeeType employee_2;

Both these syntax are useful depending on the use case.

SRK
  • 308
  • 3
  • 16