Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Array Initialization problems.

Reply
Thread Tools

Array Initialization problems.

 
 
Trevor Qi
Guest
Posts: n/a
 
      05-14-2004
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
 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      05-14-2004
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Trevor Qi wrote:

> 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+5JF4zrFcV5KOHw8r q2SnOgUQCg3qUf
62gOL0i9VLRUrwkhV++G1zY=
=Z3w1
-----END PGP SIGNATURE-----
 
Reply With Quote
 
 
 
 
Victor Nazarov
Guest
Posts: n/a
 
      05-14-2004
Trevor Qi wrote:
> 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

 
Reply With Quote
 
Mark A. Odell
Guest
Posts: n/a
 
      05-14-2004
(Trevor Qi) wrote in
news: om:

> 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;
}

--
- Mark ->
--
 
Reply With Quote
 
Tim Orbaker
Guest
Posts: n/a
 
      05-14-2004
Trevor Qi wrote:
> 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

 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      05-14-2004
In 'comp.lang.c', (Trevor Qi) wrote:

> 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[i]);
}
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[i]);
}
return 0;
}

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      05-14-2004
Trevor Qi wrote:

> 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;
}

 
Reply With Quote
 
August Derleth
Guest
Posts: n/a
 
      05-16-2004
On Fri, 14 May 2004 22:00:16 +0400, Victor Nazarov wrote:

> Trevor Qi wrote:
>> 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.

>
> vir


--
yvoregnevna gjragl-guerr gjb-gubhfnaq guerr ng lnubb qbg pbz
To email me, rot13 and convert spelled-out numbers to numeric form.
"Makes hackers smile" makes hackers smile.

 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      05-16-2004
August Derleth <> writes:

> On Fri, 14 May 2004 22:00:16 +0400, Victor Nazarov wrote:
>
>> Trevor Qi wrote:
>>> 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);
--
"I hope, some day, to learn to read.
It seems to be even harder than writing."
--Richard Heathfield
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      05-16-2004
Martin Ambuhl <> wrote:
> Trevor Qi wrote:
> > 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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
initialization of array as a member using the initialization list aaragon C++ 2 11-02-2008 04:57 PM
array initialization in initialization list. toton C++ 5 09-28-2006 05:13 PM
Initialization of non-integral type in initialization list anongroupaccount@googlemail.com C++ 6 12-11-2005 09:51 PM
Initialization via ctor vs. initialization via assignment Matthias Kaeppler C++ 2 07-18-2005 04:25 PM
Default Initialization Vs. Value Initialization JKop C++ 10 09-22-2004 07:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57