Distributed array initialization

Discussion in 'C Programming' started by Christopher Collins, Jan 4, 2013.

  1. When declaring an array with file scope, you can specify its initial
    contents, e.g.,

    int data[6] = { 100, 200, 300, 400, 500, 600 };


    Rather than initalize the full array at its point of declaration, I
    would like to declare it in one source file, and initialize different
    parts of it throughout some other source files. Below is an example
    of what I'd like to do. This is not proper C syntax, but hopefullly
    the meaning is clear.

    /* File: a.c */
    int data[6];

    /* File: b.c */
    extern int data[6];
    data[0, 1, 2] = { 100, 200, 300 }; /* (file scope) */

    /* File: c.c */
    extern int data[6];
    data[3, 4, 5] = { 400, 500, 600 }; /* (file scope) */

    One obvious way to achieve this is to define an initialization
    function in each of b.c and c.c, and call them at the start of the
    program. However, I would prefer the array to get initialized
    automatically.

    Is there any way to do this? Any thoughts are much appreciated.
     
    Christopher Collins, Jan 4, 2013
    #1
    1. Advertising

  2. On Fri, 04 Jan 2013 07:09:22 +0000, Christopher Collins wrote:

    > When declaring an array with file scope, you can specify its initial
    > contents, e.g.,
    >
    > int data[6] = { 100, 200, 300, 400, 500, 600 };
    >
    >
    > Rather than initalize the full array at its point of declaration, I
    > would like to declare it in one source file, and initialize different
    > parts of it throughout some other source files. Below is an example of
    > what I'd like to do. This is not proper C syntax, but hopefullly the
    > meaning is clear.
    >
    > /* File: a.c */
    > int data[6];
    >
    > /* File: b.c */
    > extern int data[6];
    > data[0, 1, 2] = { 100, 200, 300 }; /* (file scope) */
    >
    > /* File: c.c */
    > extern int data[6];
    > data[3, 4, 5] = { 400, 500, 600 }; /* (file scope) */
    >
    > One obvious way to achieve this is to define an initialization function
    > in each of b.c and c.c, and call them at the start of the program.
    > However, I would prefer the array to get initialized automatically.
    >
    > Is there any way to do this? Any thoughts are much appreciated.


    No, this is not possible in C.
    First of all, there is only one place where you can initialize an object
    and that is in the definition of that object. Everything else, no matter
    how much it looks like initialization is something else (most often an
    assignment).
    A second limiting factor is that each object can only be defined once.
    And if you start to think about other cute stuff, like:

    int data[6] = { [0] = b_init_0(), [1] = b_init_1(), [2] = b_init_3(),
    [3] = c_init_3(), [4] = c_init_4(), [5] = c_init_5() };
    then you run into the issue that initializers for globals must be
    constant expressions (so function calls are out).

    Bart v Ingen Schenau
     
    Bart van Ingen Schenau, Jan 4, 2013
    #2
    1. Advertising

  3. Robert Wessel <> wrote:
    > On Fri, 4 Jan 2013 07:09:22 +0000 (UTC), Christopher Collins
    > <> wrote:


    >>When declaring an array with file scope, you can specify its initial
    >>contents, e.g.,


    >> int data[6] = { 100, 200, 300, 400, 500, 600 };


    >>Rather than initalize the full array at its point of declaration, I
    >>would like to declare it in one source file, and initialize different
    >>parts of it throughout some other source files. Below is an example
    >>of what I'd like to do. This is not proper C syntax, but hopefullly
    >>the meaning is clear.


    (snip)

    > Not really in C. One slightly different approach would be to make the
    > array an array of pointers, and then initialize the objects pointed to
    > in the different modules.


    #include <stdio.h>

    int a[]={1,2,3};
    int b[]={4,5,6};
    int *ptrs[]={a,b};


    int main() {
    int i,j;
    for(i=0;i<2;i++) for(j=0;j<3;j++) printf("%d\n",ptrs[j]);
    }

    With the right declarations, it should work with a and b initialized
    in different files. With a little more work, you can also initialize
    an array with the number of elements in each subarray. (Well, array
    of pointers, maybe.)

    -- glen
     
    glen herrmannsfeldt, Jan 4, 2013
    #3
  4. Christopher Collins

    Shao Miller Guest

    On 1/4/2013 02:09, Christopher Collins wrote:
    > When declaring an array with file scope, you can specify its initial
    > contents, e.g.,
    >
    > int data[6] = { 100, 200, 300, 400, 500, 600 };
    >
    >
    > Rather than initalize the full array at its point of declaration, I
    > would like to declare it in one source file, and initialize different
    > parts of it throughout some other source files. Below is an example
    > of what I'd like to do. This is not proper C syntax, but hopefullly
    > the meaning is clear.
    >
    > /* File: a.c */
    > int data[6];
    >
    > /* File: b.c */
    > extern int data[6];
    > data[0, 1, 2] = { 100, 200, 300 }; /* (file scope) */
    >
    > /* File: c.c */
    > extern int data[6];
    > data[3, 4, 5] = { 400, 500, 600 }; /* (file scope) */
    >
    > One obvious way to achieve this is to define an initialization
    > function in each of b.c and c.c, and call them at the start of the
    > program. However, I would prefer the array to get initialized
    > automatically.
    >
    > Is there any way to do this? Any thoughts are much appreciated.
    >


    Your particular implementation might have extensions for doing this. If
    you're not too concerned about portability, then that's fine.

    The iPXE source code does just what you're trying to do. You can see
    the discussion in ipxe/src/include/ipxe/tables.h[1]. Essentially, the
    strategy involves linker magic.

    - Shao Miller

    [1] https://git.ipxe.org/ipxe.git/blob/HEAD:/src/include/ipxe/tables.h
     
    Shao Miller, Jan 4, 2013
    #4
  5. Robert Wessel wrote:

    > Not really in C. One slightly different approach would be to make the
    > array an array of pointers, and then initialize the objects pointed to
    > in the different modules.


    This room is closed. Stop trying to be important.
     
    AliceInWonderland, Jan 6, 2013
    #5
  6. Shao Miller wrote:
    [some crap infused by "the USA"?]

    What "the president" misses: "The Presidency" is cumulative "error".

    "Mr. President, send your kids (whatever your sperm's age is now), to YOUR
    war."

    Mr. President, would YOU like to go to war? To you "go to war" is.. are you
    a "criminal" Mr. President?
    What is "a President?". Are you a war crime? What is your bullshit based
    on? And it was their fault for "not knowing the ropes of rape"?

    "Mr. President"... ain't I "a jew".
     
    AliceInWonderland, Jan 6, 2013
    #6
  7. On Jan 6, 8:00 am, "AliceInWonderland" <> wrote:
    > Robert Wessel wrote:
    > > Not really in C.  One slightly different approach would be to make the
    > > array an array of pointers, and then initialize the objects pointed to
    > > in the different modules.

    >
    > This room is closed.


    it isn't a room

    > Stop trying to be important.
     
    Nick Keighley, Jan 6, 2013
    #7
  8. Nick Keighley <> writes:
    > On Jan 6, 8:00 am, "AliceInWonderland" <> wrote:
    >> Robert Wessel wrote:
    >> > Not really in C.  One slightly different approach would be to make the
    >> > array an array of pointers, and then initialize the objects pointed to
    >> > in the different modules.

    >>
    >> This room is closed.

    >
    > it isn't a room


    Nick, please don't feed the trolls.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Working, but not speaking, for JetHead Development, Inc.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jan 7, 2013
    #8
  9. Nick Keighley wrote:
    > On Jan 6, 8:00 am, "AliceInWonderland" <> wrote:
    >> Robert Wessel wrote:
    >>> Not really in C. One slightly different approach would be to make
    >>> the array an array of pointers, and then initialize the objects
    >>> pointed to in the different modules.

    >>
    >> This room is closed.

    >
    > it isn't a room


    so now you are set to explain why it isn't? Or that I thought that it was? I
    get a chance to think things over here, and now maybe you want one too?
    Hello mr. prosecutor? All grown up now? What ever happened with that stupid
    fucking **** in collusion with your "court of law", who never had a clue
    about anthing, but she served your purposes. Was there a question in that
    somewhere?

    >
    >> Stop trying to be important.
     
    AliceInWonderland, Jan 7, 2013
    #9
  10. Keith Thompson wrote:
    > Nick Keighley <> writes:
    >> On Jan 6, 8:00 am, "AliceInWonderland" <> wrote:
    >>> Robert Wessel wrote:
    >>>> Not really in C. One slightly different approach would be to make
    >>>> the array an array of pointers, and then initialize the objects
    >>>> pointed to in the different modules.
    >>>
    >>> This room is closed.

    >>
    >> it isn't a room

    >
    > Nick, please don't feed the trolls.


    Oh, as if you are so important that I have nothing better to do than to give
    you fodder for your "superiority". Hello? Gay marriage is being accepted now
    and you don't need to hide anymore so I'm now an easy target? (I didn't
    really stoop to your level and "call you gay", did I?) "C must die", and I
    play with it in amusement ( I said that here before, yes? No mr. states
    attorney.. well yeah cuz I am Stewart Clinton, and people really like me so
    I am on a 12 step progam to notice when young pretty girls will grow up and
    be very fat ugly women, so I won't let them blow my presidential dick, or I
    have sinned?)
     
    AliceInWonderland, Jan 7, 2013
    #10
  11. On 2013-01-04, Shao Miller <> wrote:
    > Your particular implementation might have extensions for doing this. If
    > you're not too concerned about portability, then that's fine.
    >
    > The iPXE source code does just what you're trying to do. You can see
    > the discussion in ipxe/src/include/ipxe/tables.h[1]. Essentially, the
    > strategy involves linker magic.
    >
    > - Shao Miller
    >
    > [1] https://git.ipxe.org/ipxe.git/blob/HEAD:/src/include/ipxe/tables.h


    Thanks, Shao (and everyone else who responded). Luckily, I don't
    actually require this type of array initialization; I just thought it
    would be nice to have. A linker trick like this is a little further
    than I am willing to go, but it is interesting to see how it is done.

    I think I will just use initialization functions. With compound
    literals, it won't be too hard to tell what data the arrays (of structs)
    are being populated with.
     
    Christopher Collins, Jan 7, 2013
    #11
  12. Christopher Collins

    gwowen Guest

    On Jan 7, 8:55 am, Christopher Collins <>
    wrote:
    >
    > Thanks, Shao (and everyone else who responded).  Luckily, I don't
    > actually require this type of array initialization; I just thought it
    > would be nice to have.  A linker trick like this is a little further
    > than I am willing to go, but it is interesting to see how it is done.


    Just as non-standard, but possibly slightly more palatable than linker
    magic, GCC gives you the attribute((constructor)) to declare a
    function that should be called before main().
     
    gwowen, Jan 7, 2013
    #12
    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. JKop
    Replies:
    10
    Views:
    995
  2. Matthias Kaeppler
    Replies:
    2
    Views:
    470
    Victor Bazarov
    Jul 18, 2005
  3. Replies:
    6
    Views:
    485
    Ron Natalie
    Dec 11, 2005
  4. toton
    Replies:
    5
    Views:
    950
    Victor Bazarov
    Sep 28, 2006
  5. aaragon
    Replies:
    2
    Views:
    637
    James Kanze
    Nov 2, 2008
Loading...

Share This Page