Wait just a minute here!!!

This is a discussion on Wait just a minute here!!! within the C Programming forums, part of the General Programming Boards category; Consider the following: Code: #include <stdio.h> int main(void) { int x[50] = {2}; int i; for (i = 0; i ...

  1. #1
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065

    Wait just a minute here!!!

    Consider the following:

    Code:
    #include <stdio.h>
    int main(void)
    {
        int x[50] = {2};
        int i;
        for (i = 0; i < 50; i++)
            printf("x[%i] = %i\n", i, x[i]);
        return 0;
    }
    and. . .
    Code:
    #include <stdio.h>
    int main(void)
    {
        struct x {
            int i, j, k;
        } x = {2};
        printf("x.i = %i, x.j = %i, x.k = %i\n", x.i, x.j, x.k);
        return 0;
    }
    What do you THINK this will do?

    I have tried this and didn't get the results that I thought would happen. What I'm attempting to figure out is whether it is my compiler or me that has gone off the deep end.

    Thanks,
    Andy

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    We don't care what it does, it's YOUR homework. What do YOU think it will do, and why?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Nice to have you back (well, not nice) Quzah. . . As cheerful as ever, I see.

    EDIT:

    For the record, I am in school again. . . but not doing much in the way of programming. . . very disappointed that all the courses aren't titled "Embedded systems programming with Windows CE" or "Really Cool Embedded systems programming with Linux", but most all are management classes :::PUKE:::

    BTW, if you come across a 500+ level class that says "Intro to . . .". . . be advised, it AIN'T NO INTRODUCTION to Jack! It is more like "Blow your head off with . . .".
    Last edited by Kennedy; 04-09-2009 at 04:03 PM.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    They do exactly as I predicted... what did you predict and why?

  5. #5
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    It did what I thought it would do.

    If you wanted it to set x.i, x.j, and x.k to all equal 2 you would need:
    Code:
        struct x {
            int i, j, k;
        } x = {2, 2, 2};
    Last edited by ಠ_ಠ; 04-09-2009 at 06:40 PM.
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    I would expect the first int to be 2 and the rest to be 0, in each case.
    Is that not what you expected?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by iMalc View Post
    I would expect the first int to be 2 and the rest to be 0, in each case.
    Is that not what you expected?
    Which is correct. . . Prior to reading this site, I had always manually initialized my vars with bzero -- later memset. I saw on this site, however, the common practice of using something like:
    Code:
    struct something { int x, y } x = {0};
    and believed this to be initializing the whole structure with the 0, thus replacing the 0 with a 2 would initialize everything to a 2. . . I'm now thinking that this is actually UB and I should NEVER use this again. From reading various other sources, what I have seen is that the assignment like this is NOT a guarantee, however, should _USUALLY_ work. Therefore, to assume that attempting to zero out a struct based upon this technique is dangerous and one may or may not actually get a structure that is zeroed.

    Comments?

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    If there are fewer initializers in a list than there are members of an aggregate, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
    If an object that has static storage duration is not initialized explicitly, it is initialized implicitly as if every member that has arithmetic type were assigned 0 and every member that has pointer type were assigned a null pointer constant. If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.
    http://web.archive.org/web/200502070...aft.html#3.5.7

    I do not think it was changed since this draft. So your USUALLY == Standard compliant.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why wait cursor still turning on?
    By Opariti in forum Windows Programming
    Replies: 0
    Last Post: 05-22-2009, 02:28 AM
  2. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  3. Signals, fork(), wait() and exec()
    By DJTurboToJo in forum C Programming
    Replies: 7
    Last Post: 03-18-2008, 09:14 AM
  4. Boom, Headoshot!!
    By mrafcho001 in forum A Brief History of Cprogramming.com
    Replies: 50
    Last Post: 07-21-2005, 08:28 PM
  5. anybody with experience using the wait() function?
    By flawildcat in forum C Programming
    Replies: 7
    Last Post: 04-22-2002, 02:43 PM

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