Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   default values for C struct fields (http://www.go4expert.com/forums/default-values-c-struct-fields-t8264/)

drichird 15Jan2008 13:11

default values for C struct fields
 
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?

Salem 15Jan2008 15:23

Re: default values for C struct fields
 
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++.

technosavvy 15Jan2008 15:27

Re: default values for C struct fields
 
if you want to initialize everything with a zero...
u can go for
Code:

employee emp1 = {0};
..

drichird 15Jan2008 20:11

Re: default values for C struct fields
 
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.


All times are GMT +5.5. The time now is 02:41.