structure member initialization

This is a discussion on structure member initialization within the C Programming forums, part of the General Programming Boards category; Hello, I am trying to declare a structure type and have the members initialized to particular values such as shown ...

  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
    zen
    zen is offline
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 09:18 PM
  2. Regarding accessing the structure member
    By kollurisrinu in forum C Programming
    Replies: 5
    Last Post: 06-18-2008, 05:51 AM
  3. Replies: 1
    Last Post: 04-17-2008, 08:45 PM
  4. Builder 5 v's bcc32.exe
    By sononix in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2004, 11:17 AM
  5. Allocating memory for a structure member
    By dalek in forum C++ Programming
    Replies: 2
    Last Post: 05-15-2003, 07:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21