This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
RE: static initialization of an array
- From: "Huber, George CECOM RDEC STCD SRI" <George dot Huber at mail1 dot monmouth dot army dot mil>
- To: "'Massimiliano Cialdi'" <cialdi at firenze dot net>, gcc <gcc at gcc dot gnu dot org>, linux c programming <linux-c-programming at vger dot kernel dot org>
- Date: Tue, 22 Jan 2002 10:03:13 -0500
- Subject: RE: static initialization of an array
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