Global structure variable initialization

Discussion in 'C Programming' started by koffee, Dec 13, 2007.

  1. koffee

    koffee Guest

    Hi people,

    I've got a question on global structure initialization.

    A global variable like int can be explicitly initialized as:
    int Global = 1;

    But how can I explicitly initialize a self-defined global structure?
    like:

    typedef mystruct {
    int a;
    int b;
    //...
    } Mystruct;

    I searched this group and heard that a global variable will be
    initialized to zero or NULL implicitly. But what if members in a
    global structure needs to be initialized to values other than 0???

    Thanks every one!!!
     
    koffee, Dec 13, 2007
    #1
    1. Advertising

  2. koffee

    koffee Guest

    Sorry I made a mistake in the struct def.

    typedef struct mystruct {
    int a;
    int b;
    //...

    } Mystruct;

    On Dec 13, 3:40 pm, koffee <> wrote:
    > Hi people,
    >
    > I've got a question on global structure initialization.
    >
    > A global variable like int can be explicitly initialized as:
    > int Global = 1;
    >
    > But how can I explicitly initialize a self-defined global structure?
    > like:
    >
    > typedef mystruct {
    > int a;
    > int b;
    > //...
    >
    > } Mystruct;
    >
    > I searched this group and heard that a global variable will be
    > initialized to zero or NULL implicitly. But what if members in a
    > global structure needs to be initialized to values other than 0???
    >
    > Thanks every one!!!
     
    koffee, Dec 13, 2007
    #2
    1. Advertising

  3. koffee

    Eric Sosman Guest

    koffee wrote:
    > Sorry I made a mistake in the struct def.
    >
    > typedef struct mystruct {
    > int a;
    > int b;
    > //...
    >
    > } Mystruct;
    >
    > On Dec 13, 3:40 pm, koffee <> wrote:
    >> Hi people,
    >>
    >> I've got a question on global structure initialization.
    >>
    >> A global variable like int can be explicitly initialized as:
    >> int Global = 1;
    >>
    >> But how can I explicitly initialize a self-defined global structure?
    >> like:
    >>
    >> typedef mystruct {
    >> int a;
    >> int b;
    >> //...
    >>
    >> } Mystruct;
    >>
    >> I searched this group and heard that a global variable will be
    >> initialized to zero or NULL implicitly. But what if members in a
    >> global structure needs to be initialized to values other than 0???


    Mystruct thing = { 42, 29, //...
    };

    This initializes thing.a to 42, thing.b to 29, ...

    --
    Eric Sosman
    lid
     
    Eric Sosman, Dec 13, 2007
    #3
  4. koffee

    koffee Guest

    On Dec 13, 4:08 pm, Eric Sosman <> wrote:
    > koffee wrote:
    > > Sorry I made a mistake in the struct def.

    >
    > > typedef struct mystruct {
    > > int a;
    > > int b;
    > > //...

    >
    > > } Mystruct;

    >
    > > On Dec 13, 3:40 pm, koffee <> wrote:
    > >> Hi people,

    >
    > >> I've got a question on global structure initialization.

    >
    > >> A global variable like int can be explicitly initialized as:
    > >> int Global = 1;

    >
    > >> But how can I explicitly initialize a self-defined global structure?
    > >> like:

    >
    > >> typedef mystruct {
    > >> int a;
    > >> int b;
    > >> //...

    >
    > >> } Mystruct;

    >
    > >> I searched this group and heard that a global variable will be
    > >> initialized to zero or NULL implicitly. But what if members in a
    > >> global structure needs to be initialized to values other than 0???

    >
    > Mystruct thing = { 42, 29, //...
    > };
    >
    > This initializes thing.a to 42, thing.b to 29, ...
    >
    > --
    > Eric Sosman
    >


    Ok THanks! I got it!
     
    koffee, Dec 13, 2007
    #4
  5. koffee

    Dann Corbit Guest

    "koffee" <> wrote in message
    news:...
    > Hi people,
    >
    > I've got a question on global structure initialization.
    >
    > A global variable like int can be explicitly initialized as:
    > int Global = 1;
    >
    > But how can I explicitly initialize a self-defined global structure?
    > like:
    >
    > typedef mystruct {
    > int a;
    > int b;
    > //...
    > } Mystruct;
    >
    > I searched this group and heard that a global variable will be
    > initialized to zero or NULL implicitly. But what if members in a
    > global structure needs to be initialized to values other than 0???


    typedef struct mystruct {
    int a;
    int b;
    } Mystruct;

    Mystruct foo = {a,b};

    struct foo {
    int bar;
    char *what;
    } thing = {
    42, "sneakers"
    };



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    Dann Corbit, Dec 13, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bryan Parkoff
    Replies:
    2
    Views:
    6,505
  2. Replies:
    1
    Views:
    386
    Michael Ekstrand
    Aug 21, 2005
  3. Replies:
    10
    Views:
    1,124
    Victor Bazarov
    Mar 15, 2007
  4. jubelbrus
    Replies:
    5
    Views:
    619
    JohnQ
    Jul 20, 2007
  5. jubelbrus

    Global variable initialization

    jubelbrus, Jul 18, 2007, in forum: C++
    Replies:
    2
    Views:
    345
    John Harrison
    Jul 18, 2007
Loading...

Share This Page