typedef struct
{
int id = 0;
char *name = NULL;
char *department = NULL;
int phone = 0;
} emp;
In C programming is it a good programming practice to do something like that, or, should I initialize when I declare the variable 'emp'.
I am using a GCC compiler and the above code does compile. I want to know if it is the proper way of initializing.
With typedef struct { ... } emp;
you are creating a new complex type called "emp". When you declare a variable of type "emp", that is where you typically initialize it.
I would go with:
typedef struct
{
int id;
char *name;
char *department;
int phone;
} emp;
emp myVar = {
/* id */ 0,
/* name */ NULL,
/* department */, NULL,
/* phone */ 0
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With