Thread: declare and initialize struct members

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    12

    declare and initialize struct members

    hello,

    thats maybe a stupid question but i got stuck on it. is there any way to not only declare but initialize array/pointer members in a struct or union?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No and yes.

    You can't initialize ANYTHING in a struct or union declaration.

    You can initialize into a variable declared of a struct or union type.

    For example:
    Code:
    struct something {
        int x;
        int *ptr;
    };
    
    int someint;
    
    struct something somevar = { 0, &someint };
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    12
    my boss leaved me confused, now this make sense to me.

    thanks mats for helping me out with this.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can also use ZeroMemory or memset to 0 out everything in a struct. This is always recommended if you're not initializing the struct as outlined above (since some member may have junk data otherwise).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-10-2009, 02:20 AM
  2. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 05:40 PM
  3. Help with classes & strings members
    By GrNxxDaY in forum C++ Programming
    Replies: 7
    Last Post: 07-21-2002, 08:29 PM
  4. more with my 1st structure program
    By sballew in forum C Programming
    Replies: 42
    Last Post: 10-22-2001, 08:03 PM