473,480 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Array Initialization problems.

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
Nov 14 '05 #1
10 1416
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

Nov 14 '05 #2
qh****@hotmail.com (Trevor Qi) wrote in
news:17**************************@posting.google.c 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 ->
--
Nov 14 '05 #3
-----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-----
Nov 14 '05 #4
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

Nov 14 '05 #5
In 'comp.lang.c', qh****@hotmail.com (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/
Nov 14 '05 #6
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;
}

Nov 14 '05 #7
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.

Nov 14 '05 #8
August Derleth <se*@sig.now> 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
Nov 14 '05 #9
Martin Ambuhl <ma*****@earthlink.net> 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)
Nov 14 '05 #10
ol*****@inspire.net.nz (Old Wolf) writes:
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.
--
Here's a tip: null pointers don't have to be *dull* pointers!
Nov 14 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5555
by: Fred Zwarts | last post by:
If I am right, members of a class that are const and not static must be initialized in the initialization part of a constructor. E.g. class C { private: const int I; public: C(); };
13
26968
by: simondex | last post by:
Hi, Everyone! Does anyone know how to initialize an int array with a non-zero number? Thank You Very Much. Truly Yours, Simon Dexter
8
3658
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
15
4828
by: Charles Sullivan | last post by:
Assume I have a static array of structures the elements of which could be any conceivable mixture of C types, pointers, arrays. And this array is uninitialized at program startup. If later in...
3
1963
by: kk_oop | last post by:
Hi. I recently wrote a simple little template that defines an array that checks attempts to use out of bounds indexes. The only problem is that it does provide the use array style value...
5
24297
by: toton | last post by:
Hi, I can initialize an array of class with a specific class as, class Test{ public: Test(int){} }; Test x = {Test(3),Test(6)}; using array initialization list. (Note Test do NOT have a...
15
3350
by: jamx | last post by:
How can you initialize an array, in the initialization list of a constructor ?? SomeClass { public: SomeClass() : *init here* { } private: int some_array; };
6
2292
by: Trev17 | last post by:
Hello, I am trying to write a program that reads in a file, and uses a 2d array to store the highest and lowest temp from each month. It also outputs the average high and the average low. The file...
31
3177
by: siddhu | last post by:
why can't we have array of references.
152
9717
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7045
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7087
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6944
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5341
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4782
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4483
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2985
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.