Distributed array initialization

  • Thread starter Christopher Collins
  • Start date
C

Christopher Collins

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.
 
B

Bart van Ingen Schenau

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
 
G

glen herrmannsfeldt

Robert Wessel said:
On Fri, 4 Jan 2013 07:09:22 +0000 (UTC), Christopher Collins
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
 
S

Shao Miller

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
 
A

AliceInWonderland

Robert said:
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.
 
A

AliceInWonderland

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".
 
A

AliceInWonderland

Nick said:
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?
 
A

AliceInWonderland

Keith said:
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?)
 
C

Christopher Collins

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.
 
G

gwowen

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().
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top