Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to initialize a 2D static array?

Reply
Thread Tools

How to initialize a 2D static array?

 
 
Vol
Guest
Posts: n/a
 
      03-10-2007
Hi, Group,

I want to initialize a 2D static array in function 'foo ()', where
'foo' is called by another function a lot of times.
I list my implementation below but I think there are better
solutions , thanks

void foo()
{
int i, j;
static int dis_sum[100][100] = {18*90};
static int tmp = 0;

if (tmp == 0)
{
for(i = 0; i < 100; i++)
for(j = 1; j < 100; j++)
dis_sum[i][j] = 18*90;
}
tmp = 1;
}

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      03-10-2007
"Vol" <> writes:
> I want to initialize a 2D static array in function 'foo ()', where
> 'foo' is called by another function a lot of times.
> I list my implementation below but I think there are better
> solutions , thanks
>
> void foo()
> {
> int i, j;
> static int dis_sum[100][100] = {18*90};


This initializes only the first element to 18*90; the others will be
initialized to 0. Since you're going to execute the loop before using
it, you don't need to bother initializing it when it's declared.
(Since it's static, it will be implicitly initialized to all zeros.)

> static int tmp = 0;


"tmp" is a lousy name; call it something like "dis_sum_initilaized".

> if (tmp == 0)
> {
> for(i = 0; i < 100; i++)
> for(j = 1; j < 100; j++)
> dis_sum[i][j] = 18*90;
> }
> tmp = 1;
> }


100 is a "magic number"; if you later change the size of the array,
you'll have to change the values in several places in your code. Use
declared constants, most likely macros. Likewise for 18*90.

Apart from that, it looks like a reasonable way to initialize a static
object when the initial value is too complex for an actual
initializer. (It would be nice if there were a syntax for "initialize
all elements to 18*90".)

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize Tony Morris Java 3 02-04-2006 08:39 AM
accessing static constant does not initialize class Tarlika Elisabeth Schmitz Java 2 12-12-2004 12:15 AM
How to initialize static ResourceBundle? J. Bayjan Java 1 01-27-2004 12:31 AM
Initialize an static array in a class RCAJ C++ 6 12-28-2003 04:31 PM



Advertisments