is this strcture initialization incorrect?

This is a discussion on is this strcture initialization incorrect? within the C++ Programming forums, part of the General Programming Boards category; Hi I had the structure TIME: Code: struct TIME {int h, m, float s;}; I initialized the variable timenow of ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    is this strcture initialization incorrect?

    Hi

    I had the structure TIME:
    Code:
    struct TIME {int h, m, float s;};
    I initialized the variable timenow of type TIME as:

    Code:
    TIME timenow;
    timenow.h = 2; timenow.m = 5; timenow.s = 30;
    The instructor deducted the marks for this initialization with the reason that I should have declared it this way:

    Code:
    TIME timenow = { 2 , 5 , 30};
    But is my initialization incorrect? Does it initialize the variable? Please guide me. Thanks.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,170
    Both methods end up with the same result, but the corrected method is a literal initialization, where as yours is just setting the struct members on a non-initialized variable.
    If you understand what you're doing, you're not learning anything.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    On the strict definition of "initialization", no your code does not initialize. Your code runs the default initializer, and then later assigns (not initializes) the values to be 2, 5, 30. (EDIT: Also, you may or may not even have a default initializer available, if no constructor with () was defined.)
    Last edited by tabstop; 05-18-2011 at 01:57 PM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,829
    It might not be a good idea to use strictly uppercase for your structs. It's generally reserved for macros.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by tabstop View Post
    On the strict definition of "initialization", no your code does not initialize. Your code runs the default initializer, and then later assigns (not initializes) the values to be 2, 5, 30. (EDIT: Also, you may or may not even have a default initializer available, if no constructor with () was defined.)
    Thank you, everyone.

    tabstop: I'm sorry I couldn't understand the bold parts. Could you please explain them a bit? Thanks.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    My EDIT should probably be ignored, as you have a POD struct with nothing fancy. But anyway: there's the idea of "default initializations", which is if you just type a variable declaration is what you get.
    Code:
    int main() {
        char a;
        int b;
        std::string c;
    }
    Since I didn't specify any initializers for these variables, they are all default-initialized. For basic types like char and int, default initialization is no initialization at all -- whatever value that chunk of memory had beforehand is still there. For a class like std::string, there's a constructor that takes no arguments that is run when you do default initialization. When you declare a struct with no initializers like you did, each of the members is default-initialized (in your case, to random numbers).

  7. #7
    Registered User xentaka's Avatar
    Join Date
    May 2011
    Posts
    60
    I realize in C++ structs are just like classes, and I just got done reading about this last night. Pretty much you rely on the default constructor when assigning vars inside the object, but when you create a constructor the compiler says "get lost" and takes away the default constructor you had been relying upon.
    Last edited by xentaka; 05-20-2011 at 06:39 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Value is incorrect??
    By Myca in forum C Programming
    Replies: 3
    Last Post: 02-11-2011, 09:20 AM
  2. RPN incorrect output
    By csharp100 in forum C Programming
    Replies: 5
    Last Post: 10-14-2010, 12:36 PM
  3. Incorrect use of “For Loop”
    By ggraz in forum C Programming
    Replies: 3
    Last Post: 10-08-2008, 08:16 PM
  4. Replies: 3
    Last Post: 12-20-2007, 02:28 PM
  5. Help in passing a strcture in function
    By sachin_powale in forum C Programming
    Replies: 10
    Last Post: 05-04-2005, 11:23 AM

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