473,763 Members | 6,149 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to initialize a 2D static array?

Vol
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;
}

Mar 10 '07 #1
1 4267
"Vol" <vo********@gma il.comwrites:
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_initil aized".
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_Keit h) ks***@mib.org <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"
Mar 10 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
14841
by: Kyle | last post by:
Hi, Is it possible to initialize a constant memeber array in a class? I tried several syntax but all failed. class A { public: A(); ~A(); private:
2
57995
by: slack_justyb | last post by:
Hello, I'm trying to figure the best way of doing the following. *I need an array of strings that are globally accessable from within 'arrayhere.h' *I need that array of strings to not change once initialized 'const' *I would like to initialize said array with a one liner, but if not something extremly easy to read. *I need to know how many elements there are in the array so I can stop
5
4572
by: Jim Langston | last post by:
What I want to do: have a vector of ints in my class initialized with 0 to 499 which will later be pushed/popped out of the vector by instances. What I have: class CParticleStream // Yes, I know you wouldn't use 'C' { private: static std::vector<int> PSArray; public:
10
43471
by: Yang Lee | last post by:
hi is this correct char teststr="\0"; or is there another method to initialise; thanks lee
15
5323
by: Geoff Cox | last post by:
Hello, Can I separately declare and initialize a string array? How and where would I do it in the code below? It was created using Visual C++ 2005 Express Beta 2 ... In C# I would have private string myArray;
18
4215
by: toton | last post by:
Hi, In C++ when I initialize an array it, also initializes the class that it contains, which calls the default constructor. However, I want to initialize the array only (i.e reserve the space) and use my specific constructor to initialize the class. How to do it without using malloc? Something like Point* pt = new Point; I want it to reserve the space for N points only, and not to call default constructor. I dont have a default...
15
26435
by: thinktwice | last post by:
char a = { 0 } is it ok?
18
9124
by: Ehud Shapira | last post by:
Is it possible to have a declaration of a struct pointer initialized to an unnamed struct? (I'm only concerned with static/global variables, if it matters.) I'm trying to do something like: struct st_a { int i, j; };
8
14731
by: aaragon | last post by:
Hi, just a very simple question. I was wondering what is the most efficient way of initializing an array. I have a very simple class that wraps an array to provide bound checking, something like this: enum checkMode {CHECK, NOCHECK}; template <int n, checkMode c = CHECK> struct ArrayStructure { static const short dim_ = n;
0
9386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9997
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9937
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8821
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6642
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.