1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

default values for C struct fields

Discussion in 'C' started by drichird, Jan 15, 2008.

  1. drichird

    drichird New Member

    Joined:
    Jan 15, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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?
     
  2. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    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++.
     
  3. technosavvy

    technosavvy New Member

    Joined:
    Jan 2, 2008
    Messages:
    52
    Likes Received:
    0
    Trophy Points:
    0
    if you want to initialize everything with a zero...
    u can go for
    Code:
    employee emp1 = {0};
    ..
     
  4. drichird

    drichird New Member

    Joined:
    Jan 15, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    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.
     

Share This Page