How can I initialize an array? 
Author Message
 How can I initialize an array?

Hi!

Is there any way of initializing an array without having to type the
content of each element?

I mean, suppose I want do declare an array of 40 integers, and I want all
these integers to be "345".

Is there any way of initilizing the array without using a for() loop or a
function like memset?

Kind regards,
Fernando Ariel Gont

--



Sat, 03 May 2003 03:00:00 GMT  
 How can I initialize an array?

Quote:

> Is there any way of initilizing the array without using a for() loop
> or a
> function like memset?

Probably not, unless perhaps you want to resort to extremely unportable
tricks and games.  Why would you need some other method?

--

 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ I am become death, the destroyer of worlds.
\__/ J. Robert Oppenheimer (quoting Hindu legend)
    Polly Wanna Cracka? / http://www.*-*-*.com/
 The Internet resource for {*filter*} relationships.
--



Sun, 04 May 2003 03:00:00 GMT  
 How can I initialize an array?

Quote:

> Is there any way of initializing an array without having to type the
> content of each element?

Not unless the value you want is 0.

Richard
--



Sun, 04 May 2003 03:00:00 GMT  
 How can I initialize an array?

Quote:

> Hi!

> Is there any way of initializing an array without having to type the
> content of each element?

> I mean, suppose I want do declare an array of 40 integers, and I want all
> these integers to be "345".

> Is there any way of initilizing the array without using a for() loop or a
> function like memset?

a[ 0] = 345;  a[ 1] = 345;  a[ 2] = 345;  a[ 3] = 345;
a[ 4] = 345;  a[ 5] = 345;  a[ 6] = 345;  a[ 7] = 345;
a[ 8] = 345;  a[ 9] = 345;  a[10] = 345;  a[11] = 345;
a[12] = 345;  a[13] = 345;  a[14] = 345;  a[15] = 345;
a[16] = 345;  a[17] = 345;  a[18] = 345;  a[19] = 345;
a[20] = 345;  a[21] = 345;  a[22] = 345;  a[23] = 345;
a[24] = 345;  a[25] = 345;  a[26] = 345;  a[27] = 345;
a[28] = 345;  a[29] = 345;  a[30] = 345;  a[31] = 345;
a[32] = 345;  a[33] = 345;  a[34] = 345;  a[35] = 345;
a[36] = 345;  a[37] = 345;  a[38] = 345;  a[39] = 345;

--
Richard Heathfield
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
--



Sun, 04 May 2003 03:00:00 GMT  
 How can I initialize an array?

Quote:

> Is there any way of initializing an array without having to type the
> content of each element?

No. Unless you want the initial value to be zero.

Quote:
> Is there any way of initilizing the array without using a for() loop
> or a function like memset?

Yes. You can use a while() or do .. while() loop, or even a loop made
with 'goto' ;-> But that's clearly not what you're looking for.

C doesn't have implicit loops in data initializers, like e.g. fortran
does.  I'm glad it doesn't.
--

Even if all the snow were burnt, ashes would remain.
--



Sun, 04 May 2003 03:00:00 GMT  
 How can I initialize an array?
Hey fernando,

and I thought I should not post my idea - simple and portable form
of precompiler abuse...

#define _10X(X) X,X,X,X,X, X,X,X,X,X
a[] = { _10X(345), _10X(345), _10X(345), _10X(345) };

this one is just enough for most of the problems that people have.

Otherwise, I think nobody else has been going to ask, *why* you would
want to have an array filled allover with the same nonzero value. The
advantage of a global static without init is - that it is put into
a BSS section, so it does not fill your executable *and* it will be
zero. In all the cases I can think of, it came about to be a good style
to change the work-functions on the array, so that their computational
null-mark *is* 345. Even more, in most of the cases I came across, the
initial value was not something like 345, but it was -1 (the usual
posix error for everything). This is sometimes a nice wish to have
fulfilled, but all you can do is a kind of if-static-init

function ()
{
  static field[MAXFIELD];
  static field_ready;
  if (!field_ready) { for (...) ...; field_ready++; }

  use(field);

Quote:
}

The latter is seen quite often, especially for some complex
structures whose actual member-layout is not known by the
function to be "stable", so that it is not feasible to use
the curly-brace initializer anyway.

brgds
-- guido                                Edel sei der Mensch, hilfreich und gut

--



Mon, 05 May 2003 08:43:58 GMT  
 How can I initialize an array?

Quote:


> > Is there any way of initializing an array without having to type the
> > content of each element?

> No. Unless you want the initial value to be zero.

> > Is there any way of initilizing the array without using a for() loop
> > or a function like memset?

> Yes. You can use a while() or do .. while() loop, or even a loop made
> with 'goto' ;-> But that's clearly not what you're looking for.

Since you got there first with the facetious while answer, I had
to come up with something else silly.  This sets a[i] = i, i.e.
non-zero values.

#define SIZE 100

int a[SIZE];

void inita(int sz)
{
   if (sz--) {
      a[sz] = sz;
      inita(sz);
   }

Quote:
} /* inita */

int main(void)
{
    inita(SIZE);
    /* ... */

Quote:
} /* main */

(UNTESTED code, but I think it will compile and run first shot)

--

     http://www.qwikpages.com/backstreets/cbfalconer
     (Remove "NOSPAM." from reply address. Above works unmodified)
--



Mon, 05 May 2003 08:44:18 GMT  
 How can I initialize an array?

Quote:
> Is there any way of initializing an array without having to type the
> content of each element?

> I mean, suppose I want do declare an array of 40 integers, and I want all
> these integers to be "345".

> Is there any way of initilizing the array without using a for() loop or a
> function like memset?

Why not use a loop? The performance penalty for any reasonable sized array
is utterly negligible. I suspect that with current processors it even is
faster than something like:

    int TheArray[ 40 ];

    int * ArPtr = TheArray;

    *ArPtr = 345;
    *(ArPtr+1) = *ArPtr;
    memcpy( ArPtr+2, ArPtr, 2 * sizeof( int ));
    memcpy( ArPtr+4, ArPtr, 4 * sizeof( int ));
    memcpy( ArPtr+8, ArPtr, 8 * sizeof( int ));
    memcpy( ArPtr+16, ArPtr, 16 * sizeof( int ));
    memcpy( ArPtr+32, ArPtr, 10 * sizeof( int ));

    /* 2+4+8+16+10 == 40 ... I hope ... */

--
Greetings from
 _____
 /_|__| Auke Reitsma, Delft, The Netherlands.
/  | \  -------------------------------------
        Remove SPAMBLOCK from my address ...
--



Mon, 05 May 2003 08:44:29 GMT  
 How can I initialize an array?
What's wrong with using memset()?

for example,

int a[40];
memset(a, 345, 40* sizeof(int));

Quote:

> Hi!

> Is there any way of initializing an array without having to type the
> content of each element?

> I mean, suppose I want do declare an array of 40 integers, and I want all
> these integers to be "345".

> Is there any way of initilizing the array without using a for() loop or a
> function like memset?

> Kind regards,
> Fernando Ariel Gont

> --


--



Mon, 05 May 2003 03:00:00 GMT  
 How can I initialize an array?

Quote:

> What's wrong with using memset()?

That it simply won't work, unless the array you want to initialize
happens to contain chars.

Quote:
> int a[40];
> memset(a, 345, 40* sizeof(int));

On the probably overwhelming majority of platforms, this doesn't
work. To start with '345' is not a char value on most of them, and
even if it is, there's still a good chance that sizeof(int)!=1. And
only if it's ==1, this code would do what was asked for.

--

Even if all the snow were burnt, ashes would remain.
--



Tue, 06 May 2003 03:00:00 GMT  
 How can I initialize an array?


Quote:
> What's wrong with using memset()?

> for example,

> int a[40];
> memset(a, 345, 40* sizeof(int));


> > Hi!

> > Is there any way of initializing an array without having to type the
> > content of each element?

> > I mean, suppose I want do declare an array of 40 integers, and I
want all
> > these integers to be "345".

> > Is there any way of initilizing the array without using a for()
loop or a
> > function like memset?

memset only works on chars, so unless sizeof(int) == sizeof(char), you
are not going to get int's with value 345. If you mean a function other
than memset, then how do you write it without a for() loop &c ?

--
Tristan Styles #1485

Failure is not an Option
It is Standard Operating Procedure
--



Tue, 06 May 2003 03:00:00 GMT  
 How can I initialize an array?
Tim schrieb:

Quote:

> What's wrong with using memset()?

> for example,

> int a[40];
> memset(a, 345, 40* sizeof(int));

On most implementations 345 won't fit into an unsigned char, so the
pattern will not be what you expect. However it _could_ work on some
others where CHAR_BIT > 8

it is not guaranteed to work for anything except unsigned char
arrays and unsignes ints.
Definitely not on arrays of pointers, floats, doubles and most types
of structs.

Kind regards
Robert

<snip>
--

I don't make my mistakes more than once. I store them carefully and
after some time I take them out again, add some new features and
_reuse_ them.
--



Tue, 06 May 2003 03:00:00 GMT  
 How can I initialize an array?

Quote:

> What's wrong with using memset()?

> for example,

> int a[40];
> memset(a, 345, 40* sizeof(int));

Try it. Test it. I'll bet you you don't get the answer you counted on
getting.

Hey, I'll save you some typing...

#include <stdio.h>
#include <string.h>

int main(void)
{
  int a[40];
  int i;
  memset(a, 345, sizeof a); /* or, if you prefer, 40 * sizeof(int). It's
the same number */
  for(i = 0; i < 40; i++)
  {
    if(a[i] == 345)
    {
      printf("Hey, Tim's right!\n");
    }
    else
    {
      printf("%d? Oops, Tim's wrong.\n", a[i]);
    }
  }
  return 0;

Quote:
}

--
Richard Heathfield
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
--



Tue, 06 May 2003 03:00:00 GMT  
 How can I initialize an array?

Quote:

> What's wrong with using memset()?

> for example,

> int a[40];
> memset(a, 345, 40* sizeof(int));

That sets all the _bytes_ to 345 (which, of course, is likely, though
not guaranteed, to be far too much for a byte, and therefore to be
converted to unsigned char, usually to 89), not all the ints.

Richard
--



Tue, 06 May 2003 03:00:00 GMT  
 How can I initialize an array?

Quote:
>What's wrong with using memset()?
>for example,
>int a[40];
>memset(a, 345, 40* sizeof(int));

I just wondered if there was any way of doing it without that
function.

Kind regards,

Fernando Ariel Gont

[To send a personal reply, please remove the ANTISPAM tag]
--



Tue, 06 May 2003 03:00:00 GMT  
 
 [ 26 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Initializing a wchar_t __gc[] Array

2. Initialize array from members of a struct: Legal ?

3. initializing an array using specific subscripts

4. Initializing array elements in ctor-initial

5. How to initialize a static array ?

6. initialize char-array with string

7. Initializing static arrays

8. initializing a const char array

9. How to initialize a 6*6 array

10. Initializing Character arrays (HELP)

11. Initializing a Type[] array

12. array initialize to zero

 

 
Powered by phpBB® Forum Software