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

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
yogisha
  • 92
  • 1
  • 9
  • Which version of GCC are you using on which platform? It should not compile in any version. It does not compile with GCC 4.8.2 on Mac OS X 10.9.2 even with none of the strictures I normally use. I get the error `duff.c:3:12: error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token int id = 0;` – Jonathan Leffler Feb 27 '14 at 04:26

5 Answers5

6

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 
};
GrandAdmiral
  • 1,348
  • 2
  • 24
  • 52
1

Since the syntax you show won't compile in a C compiler — nor a C++ compiler, AFAIK — you don't have any choice in the matter. You can't do what you are trying to do and must initialize when you declare a variable of type emp.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • @yogisha: See my comment to the question. Frankly, I don't believe you, but please provide the details of compiler version, platform where you're running it, and any options other than `-c` that you're using to get the source to compile. I took the code from the question and placed it into a file `duff.c` and tried to compile it. Even at its laxest, GCC 4.8.2 won't compile the code. – Jonathan Leffler Feb 27 '14 at 04:27
  • gcc version 4.1.2 20080704 (Red Hat 4.1.2-44) , I placed the code in a 'header.h' file and included the file in main.c, Used 'gcc -c main.c -I../include/' to compile – yogisha Feb 27 '14 at 04:30
  • I would hazard a guess that you're using a different `header.h`. I don't have a copy of GCC 4.1.2 any more; the oldest version I have on hand is 4.4.2…oh, unless I try that other VM…pause for reactivation (and upgrade, I expect) of the VM…a SuSE 10.5 VM has 'gcc (GCC) 4.1.2 20070115 (SUSE Linux)' and it generates the message: `duff.c:3: error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token`. Granted, not identical to your compiler, but very similar. – Jonathan Leffler Feb 27 '14 at 04:34
  • I'm not going to initialze like that anyways, thank you – yogisha Feb 27 '14 at 04:42
  • @yogisha: OK; you certainly aren't going to initialize as shown in the question. As a point of detail, I said 'SuSE 10.5 VM'; that's incorrect — it is SuSE 10SP2. It doesn't make very much difference. Good luck. Note that you can specify `-std=c99` and use designated initializers if you want: `static emp employee = { .id = 1, .name = "no-one", .department = "mythology", .phone = "999-888-7777" };`. You don't even have to have them in the correct order, though it is easiest if they are. – Jonathan Leffler Feb 27 '14 at 04:48
  • @JonathanLeffler The given code in the OP compiles even in MSVS 2017. I didn't know it was a bad practice (or basically a non valid one in C) because of this. – SarpSTA Mar 31 '18 at 19:23
  • @SarpSTA: ‘Tis interesting that it compiles in VS 2017. I think it would not have compiled in those available when the question was asked in 2014. – Jonathan Leffler Mar 31 '18 at 19:37
  • @JonathanLeffler I can't talk for 2014 but it compiles on 2015 version too. I don't know about the compiler MSVS uses but it probably is a C++ compiler. – SarpSTA Mar 31 '18 at 21:01
0

Better to initialize when you declare emp

user376507
  • 2,004
  • 1
  • 19
  • 31
0

In plain C, you can't give default values to struct members. If it's useful to create it with some default values, you could write a function like this:

void emp_set_defaults(emp *e)
{
    assert(e != NULL);
    e->id = 0;
    e->name = NULL;
    e->department = NULL;
    e->phone = 0;
}

and use it like

emp e;
emp_set_defaults(&e);
Chris McGrath
  • 1,936
  • 16
  • 17
0

In C99 you can use designated initializers to create a struct object with its members filled in by name. Here's an example:

emp myVar = { 
  .id = 0, 
  .name = NULL, 
  .department = NULL, 
  .phone = 0 
};

More info at C struct initialization using labels. It works, but how? Documentation?

Community
  • 1
  • 1
StilesCrisis
  • 15,972
  • 4
  • 39
  • 62