Array Initialization problems.

T

Trevor Qi

Hi,dear all:

I have the following question. If I want to initialize an array using
braces, is there a way that I can define them and give the initial
value in different lines, like:

int X[5];
X[5] = {0,0,1,2,9};

But this gives me error message. I know "int X[5] = {0,0,1,2,9};"
works.
I really want to do this because this is a public array and will be
used by many Subroutines.

Thanks!

Sincerely,
Trevor
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Trevor said:
Hi,dear all:

I have the following question. If I want to initialize an array using
braces, is there a way that I can define them and give the initial
value in different lines, like:

int X[5];
X[5] = {0,0,1,2,9};

Not unless you use a typographical break rather than a statement break.

You'd have to do something like

int X[5]
= {0,0,1,2,9};

Otherwise, what you want to do is not possible, and your code sample is
incorrect.
But this gives me error message. I know "int X[5] = {0,0,1,2,9};"
works.
I really want to do this because this is a public array and will be
used by many Subroutines.

Thanks!

Sincerely,
Trevor


- --

Lew Pitcher, IT Consultant, Enterprise Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFApQjtagVFX4UWr64RAgQiAJ0XI+5JF4zrFcV5KOHw8rq2SnOgUQCg3qUf
62gOL0i9VLRUrwkhV++G1zY=
=Z3w1
-----END PGP SIGNATURE-----
 
V

Victor Nazarov

Trevor said:
Hi,dear all:

I have the following question. If I want to initialize an array using
braces, is there a way that I can define them and give the initial
value in different lines, like:

int X[5];
X[5] = {0,0,1,2,9};
As I know, there isn't, even in C99.

Explain any reaseon why you want to do it in two lines.

vir
 
M

Mark A. Odell

(e-mail address removed) (Trevor Qi) wrote in

Hi,dear all:

I have the following question. If I want to initialize an array using
braces, is there a way that I can define them and give the initial
value in different lines, like:

int X[5];
X[5] = {0,0,1,2,9};

But this gives me error message. I know "int X[5] = {0,0,1,2,9};"
works.
I really want to do this because this is a public array and will be
used by many Subroutines.

Then no, you cannot. You could do something silly like:

x[0] = 0, x[1] = 0, x[2] = 1, x[3] = 2, x[4] = 9;

but that's probably not something that will scale too well. If x[] is
file-scoped you could create a function level array and the memcpy() it.
E.g.

#include <string.h>

int x[5];

int main(void)
{
int private_x[] = { 0, 0, 1, 2, 9 };

memcpy(x, private_x, sizeof x);

return 0;
}
 
T

Tim Orbaker

Trevor said:
Hi,dear all:

I have the following question. If I want to initialize an array using
braces, is there a way that I can define them and give the initial
value in different lines, like:

int X[5];
X[5] = {0,0,1,2,9};

But this gives me error message. I know "int X[5] = {0,0,1,2,9};"
works.
I really want to do this because this is a public array and will be
used by many Subroutines.

It sounds to me like this is what you want (tested with gcc):

In a header named 'array.h', include the line:

extern int X[5];

Then, create another file (perhaps: array.c), include:

int X[5] = { 0, 0, 1, 2, 9 };

Then either #include 'array.h' in each file that uses this array or use:

extern int X[5];

in any lexical scope that uses the array.

Tim
 
E

Emmanuel Delahaye

In said:
Hi,dear all:

I have the following question. If I want to initialize an array using
braces, is there a way that I can define them and give the initial
value in different lines, like:

int X[5];
X[5] = {0,0,1,2,9};

But this gives me error message. I know "int X[5] = {0,0,1,2,9};"
works.
I really want to do this because this is a public array and will be
used by many Subroutines.

If it is a read-only data, you can define it

/* in a unique compile unit */
static int const X[] = {0,0,1,2,9};

or

/* shared constant */
int const X[];

with this declaration:
extern int const X[] = {0,0,1,2,9};

Example :

#include <stdio.h>

extern int const X[] = {0,0,1,2,9};

int const X[];

int main (void)
{
int i;

for (i = 0; i < sizeof X / sizeof *X; i++)
{
printf ("X[%d] = %d\n", i, X);
}
return 0;
}

D:\CLC\T\TREVOR>bc
X[0] = 0
X[1] = 0
X[2] = 1
X[3] = 2
X[4] = 9

Or

/* const.c */
#include "const.h"

int const X[];

#ifndef H_ED_CONST_20040514223101
#define H_ED_CONST_20040514223101

/* const.h */
extern int const X[] = {0,0,1,2,9};

#endif /* guard */

/* Guards added by GUARD (c) ED 2000-2003 May 09 2004 Ver. 1.6 */

/* main.c */
#include <stdio.h>
#include "const.h"

int main (void)
{
int i;

for (i = 0; i < sizeof X / sizeof *X; i++)
{
printf ("X[%d] = %d\n", i, X);
}
return 0;
}
 
M

Martin Ambuhl

Trevor said:
Hi,dear all:

I have the following question. If I want to initialize an array using
braces, is there a way that I can define them and give the initial
value in different lines, like:

int X[5];
X[5] = {0,0,1,2,9};

But this gives me error message. I know "int X[5] = {0,0,1,2,9};"
works.
I really want to do this because this is a public array and will be
used by many Subroutines.

Assignment of arrays is not part of C. To copy arrays at a go, use
memmove() or memcpy().

I have given another answer in the code below, but it will *not* work
with C89 compilers, which means it will not work with most actually
existing compilers. Even when it _does_ work, it may not be the best
approach.

int main(void)
{
int *X;
X = (int [5]){ 0, 0, 1, 2, 9}; /* make the pointer (not array) X
point to an anonymous array */
return 0;
}
 
A

August Derleth

Trevor said:
Hi,dear all:

I have the following question. If I want to initialize an array using
braces, is there a way that I can define them and give the initial
value in different lines, like:

int X[5];
X[5] = {0,0,1,2,9};
As I know, there isn't, even in C99.

Then you know wrong: C99 permits compound literals.

Like so:

int *X;
X = (int [5]) {0,0,1,2,9};

Not exactly the same, but damned close.
Explain any reaseon why you want to do it in two lines.

A lot of other languages permit much the same thing.
 
B

Ben Pfaff

August Derleth said:
Trevor said:
Hi,dear all:

I have the following question. If I want to initialize an array using
braces, is there a way that I can define them and give the initial
value in different lines, like:

int X[5];
X[5] = {0,0,1,2,9};
As I know, there isn't, even in C99.

Then you know wrong: C99 permits compound literals.

Like so:

int *X;
X = (int [5]) {0,0,1,2,9};

Not exactly the same, but damned close.

Even closer:
int X[5];
memcpy(X, (const int[5]) {0, 0, 1, 2, 9}, sizeof X);
 
O

Old Wolf

Martin Ambuhl said:
Trevor said:
I have the following question. If I want to initialize an array using
braces, is there a way that I can define them and give the initial
value in different lines, like:

int X[5];
X[5] = {0,0,1,2,9};

But this gives me error message.

Assignment of arrays is not part of C. To copy arrays at a go, use
memmove() or memcpy().

int *X;
X = (int [5]){ 0, 0, 1, 2, 9};

Is it completely portable to go:

int X[5];
memcpy(X, (int []){ 0, 0, 1, 2, 9}, 5 * sizeof(int));

(I imagine that a trap representation might be generated after copying
some of the bytes of an int)
 
B

Ben Pfaff

Is it completely portable to go:

int X[5];
memcpy(X, (int []){ 0, 0, 1, 2, 9}, 5 * sizeof(int));

It's portable, but only to C99 implementations, which severely
undermines its practical value.
(I imagine that a trap representation might be generated after copying
some of the bytes of an int)

No, objects may always be safely manipulated as arrays of
character type, and this is the way that memcpy copies.
 

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,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top