default values for C struct fields

drichird's Avatar, Join Date: Jan 2008
Newbie Member
of course can't do this

typedef struct {
char name[50] = "not_set_yet";
int ssn = 111223333;
} employee;

is: employee emp1 = {"not_set_yet", 111223333}; the only way?

what is the most elegant way to have default values for all instances of that type of struct?
0
Salem's Avatar, Join Date: Nov 2007
Ambitious contributor
You write it as C++, and supply a default constructor function.

Or if you're stuck with C, then your approach is one of several roughly equal approaches.
None of which would be as elegant as using C++.
0
technosavvy's Avatar
Contributor
if you want to initialize everything with a zero...
u can go for
Code:
employee emp1 = {0};
..
0
drichird's Avatar, Join Date: Jan 2008
Newbie Member
Quote:
Originally Posted by technosavvy
if you want to initialize everything with a zero...
u can go for
Code:
employee emp1 = {0};
..
Of course I cannot use C++ (must be pure C) or question would be trivial. Thanks technosavvy, I will just zero out all fields as you suggest, makes it a little cleaner.