public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* static initialization of an array
@ 2002-01-21  9:34 Massimiliano Cialdi
  0 siblings, 0 replies; 4+ messages in thread
From: Massimiliano Cialdi @ 2002-01-21  9:34 UTC (permalink / raw)
  To: gcc, linux c programming

I use gcc 3.0.3
I must initialize a fixed size (but a priori unknow) array.
In assembler there is DUP which let to initialize a memory area with 
some value.
There is something similar in C?

thanks
-- 
Massimiliano Cialdi
cialdi@control.dsi.unifi.it
cialdi@firenze.net

^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: static initialization of an array
@ 2002-01-22  9:04 Huber, George CECOM RDEC STCD SRI
  0 siblings, 0 replies; 4+ messages in thread
From: Huber, George CECOM RDEC STCD SRI @ 2002-01-22  9:04 UTC (permalink / raw)
  To: 'Massimiliano Cialdi', gcc, linux c programming

Hi,

There are several answers to this.

1) If you want to set all bytes in the area to a given value,
   you could use memset to do this.  For example, the following
   code creates an array of 50 integers and initializes the 
   memory to zero

   #define MAX_SIZE  50
   int*     pArray;

   if(NULL != (pArray = new int[MAX_SIZE]))
   {
	memset((void*)pArray, 0, MAX_SIZE*sizeof(int));
   }

2) If you want to set each value to something different you will
   need to use a loop to do so.  For example, the following code
   creates an array of 50 integers and initializes each with its
   index.

   #define MAX_SIZE   50
   int*       pArray;

   if(NULL != (pArray = new int[MAX_SIZE]))
   {
        for(int nIdx = 0; nIdx < MAX_SIZE; nIdx++)
        {
            pArray[nIdx] = nIdx;
        }
   }

hope this helps,

George Huber
Computer Scientist
SRI, International
phone: 732-427-8064
fax    : 732-427-2065
cell   : 732-740-4018
george.huber@mail1.monmouth.army.mil


-----Original Message-----
From: Massimiliano Cialdi [mailto:cialdi@firenze.net]
Sent: Monday, January 21, 2002 11:00 AM
To: gcc; linux c programming
Subject: static initialization of an array


I use gcc 3.0.3
I must initialize a fixed size (but a priori unknow) array.
In assembler there is DUP which let to initialize a memory area with 
some value.
There is something similar in C?

thanks
-- 
Massimiliano Cialdi
cialdi@control.dsi.unifi.it
cialdi@firenze.net

-
To unsubscribe from this list: send the line "unsubscribe
linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: static initialization of an array
  2002-01-21  9:04 Massimiliano Cialdi
@ 2002-01-21  9:25 ` Jakub Jelinek
  0 siblings, 0 replies; 4+ messages in thread
From: Jakub Jelinek @ 2002-01-21  9:25 UTC (permalink / raw)
  To: Massimiliano Cialdi; +Cc: linux c programming, gcc

On Wed, Mar 03, 1999 at 04:53:42PM +0100, Massimiliano Cialdi wrote:
> I use gcc 3.0.3
> I must initialize a fixed size (but a priori unknow) array.
> In assembler there is DUP which let to initialize a memory area with 
> some value.
> There is something similar in C?

Not in ISO C, GCC has an extension where you can initialize a range:
#define SIZE 256
int foo [SIZE] = { [ 1 ... SIZE - 32 ] = 52, [ SIZE - 1 ] = 74 };
See gcc documentation.

	Jakub

^ permalink raw reply	[flat|nested] 4+ messages in thread

* static initialization of an array
@ 2002-01-21  9:04 Massimiliano Cialdi
  2002-01-21  9:25 ` Jakub Jelinek
  0 siblings, 1 reply; 4+ messages in thread
From: Massimiliano Cialdi @ 2002-01-21  9:04 UTC (permalink / raw)
  To: linux c programming, gcc

I use gcc 3.0.3
I must initialize a fixed size (but a priori unknow) array.
In assembler there is DUP which let to initialize a memory area with 
some value.
There is something similar in C?

thanks
-- 
Massimiliano Cialdi
cialdi@control.dsi.unifi.it
cialdi@firenze.net

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2002-01-22 15:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-01-21  9:34 static initialization of an array Massimiliano Cialdi
  -- strict thread matches above, loose matches on Subject: below --
2002-01-22  9:04 Huber, George CECOM RDEC STCD SRI
2002-01-21  9:04 Massimiliano Cialdi
2002-01-21  9:25 ` Jakub Jelinek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).