Thread: declare and initialize struct members

  1. #1
    Registered User
    Join Date
    Oct 2007

    declare and initialize struct members


    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
    Farncombe, Surrey, England
    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:
    struct something {
        int x;
        int *ptr;
    int someint;
    struct something somevar = { 0, &someint };
    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
    my boss leaved me confused, now this make sense to me.

    thanks mats for helping me out with this.

  4. #4
    Join Date
    Oct 2007
    Inside my computer
    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
Website Security Test