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 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 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 ->
--
-----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-----
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
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/
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;
}
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.
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
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) 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! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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();
};
|
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
|
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...
|
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...
|
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...
| |
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...
|
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;
};
|
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...
|
by: siddhu |
last post by:
why can't we have array of references.
|
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 = { {...
|
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...
| |
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,...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |