Thread: structure member initialization

  1. #1
    Registered User ivandn's Avatar
    Join Date
    Oct 2001
    Posts
    49

    structure member initialization

    Hello,



    I am trying to declare a structure type and have the members initialized to particular values such as shown below (this would not compile):



    struct MyType

    {

    int x =0;

    int y =0;

    };





    In C++ or Java I would use a constructor for a class and am looking for the equivalent in C.



    Any input would be greatly appreciated.
    Ivan

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Since a constructor is just a function you can provide your own initialisation function and explicity call it each time you create an instance of your struct.

    void YourTypeInit(YourType* a)
    {
    a->x=0;
    a->y=0;
    }
    zen

  3. #3
    Registered User ivandn's Avatar
    Join Date
    Oct 2001
    Posts
    49
    The good thing about a constructor is you dont have to call it, it gets called automatically and therefore an object can not be declared with out also being initializing preventing invalid use.

    Is there no way to provide default initialization for structure members in c?
    Ivan

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Is there no way to provide default initialization for structure members in c?
    Well there's no automatic way - that's what c++ does.

    However, you can do something like this

    Code:
    struct MyType {
    
        int x;
    
        int y;
    
    };
    
    // data - if all members are static
    struct MyType initMyType = { 0, 0 };
    
    // function - if some members are dynamic
    // like allocating space
    struct MyType initMyType ( void ) {
        struct MyType result;
        result.x = 0;
        result.y = 0;
        return result;
    }
    
    When you declare a variable, it would go something like this.
        struct MyType var1 = initMyType;
    or
        struct MyType var2 = initMyType();
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > The good thing about a constructor is you dont have to call it, it
    > gets called automatically and therefore an object can not be
    > declared with out also being initializing preventing invalid use.

    The bad thing about a constructor is that it is in C++, and we are on a C board.

    The other bad news is that Salem beat me to it.

    Quzah.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Regarding accessing the structure member
    By kollurisrinu in forum C Programming
    Replies: 5
    Last Post: 06-18-2008, 04:51 AM
  3. Replies: 1
    Last Post: 04-17-2008, 07:45 PM
  4. Builder 5 v's bcc32.exe
    By sononix in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2004, 10:17 AM
  5. Allocating memory for a structure member
    By dalek in forum C++ Programming
    Replies: 2
    Last Post: 05-15-2003, 06:56 AM