Thread: initializing of strucutre variable

  1. #1
    Registered User
    Join Date
    Jul 2007
    Location
    Hyderabad
    Posts
    35

    initializing of strucutre variable

    hi,

    why cant we initialise structure variables at the time structure declaration.
    Code:
    struct
    {
         int i=10;
         char a='s';
    };

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Because that's the way the language was designed.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Exactly what I was going to say.
    If you want to do that kind of thing, you want to be using C++, in which you can use "constructors" to achieve the desired effect.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    And constructors can be "faked" in C.

    Code:
    struct something
    {
        int x;
    };
    
    ...
    
    struct something * create_something(int x)
    {
        struct something *s = malloc(sizeof(*s));
        s->x = x;
        return s;
    }
    So.... yeah....

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    struct
    {
         int i;
         char a;
    } instance = { 10, 's' };
    
    int main()
    {
        printf ("%d %c\n", instance.i, instance.a);
        return 0;
    }

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Yes, on a per instance basic. Not default values, though, like the OP appears to want.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Think of it this way:
    Structures are blueprints on how to build your custom data type. The thing is that it's a blueprint - it doesn't contain information about what the actual objects created from it contains. So you can't modify the blueprint to hold information.
    Instead, you must first CREATE your object FROM the blueprint and initialize it, which is what robwhit just did.

  8. #8
    Registered User
    Join Date
    Jul 2007
    Location
    Hyderabad
    Posts
    35
    thank you for info.i was getting doubt on it. now iam able to understand.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MacGyver View Post
    And constructors can be "faked" in C.
    That's more like a combination of new plus a constructor call. I'm not sure I'd call it "fake," it's an extremely common pattern.

    I tend to decouple allocation from initialization, and deallocation from deinitialization, for maximum flexibility.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You are right that is new + constructor. I put fake in quotation marks because I didn't think it was the right word, but it still fit the idea. It's not a constructor, but it can be thought of as one.

    Also, I believe C++ really does use these kind of techniques, though, under the hood more or less. So the word fake becomes less appropriate if that is the case.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to put a check on an extern variable set as flag
    By rebelsoul in forum C Programming
    Replies: 18
    Last Post: 05-25-2009, 03:13 AM
  2. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  3. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  4. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  5. different way of initializing variable?
    By The Gweech in forum C++ Programming
    Replies: 3
    Last Post: 09-06-2001, 06:44 PM