473,383 Members | 1,788 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

How to run time initialize array of structures

Hi, guys, I somewhere read "You cannot initialize a structure like that at run time."
example:
Expand|Select|Wrap|Line Numbers
  1. struct item_info
  2. {
  3.       char itemname[15];
  4.       int quantity;
  5.       float retail;
  6.       float wholesale;
  7.  }item[NOOFITEM];
  8. int main()
  9. {
  10.        item[0]={"rice",10,40,30};
  11.        item[1]={"sugar",10,40,30};
  12.        item[2]={"soap",10,40,30};
  13. }
But if you want to assign values at run time then you have to do it manually like:
Expand|Select|Wrap|Line Numbers
  1. strcpy(item[0].itemname, "rice");
  2. item[0].quantity = 10;
  3. item[0].retail = 40;
  4. item[0].wholesale = 30;
i tried in internet but am unable to know the differences. I want to know the difference between those two in terms of run time and compile time.
Please explain me also the below one. Is this run time or compile time? How does we actually decide which is run time and which is compile time!
Expand|Select|Wrap|Line Numbers
  1. struct item_info
  2. {
  3.       char itemname[15];
  4.       int quantity;
  5.       float retail;
  6.       float wholesale;
  7.       //int quatityonorder;
  8. }item[NOOFITEM] =
  9. {
  10.     {"rice",10,40,30},
  11.     {"sugar",10,40,30},
  12.     {"soap",10,40,30}
  13. };
Aug 26 '14 #1

✓ answered by weaknessforcats

That is correct. It will be an array of NOOFITEM elements.

3 2996
weaknessforcats
9,208 Expert Mod 8TB
If the array exists, then you have to assign values individually.

Expand|Select|Wrap|Line Numbers
  1. item_info array[15];
  2. //At this point the array exists so you need assignment
  3. strcpy(array[0].itemname,"rice");  
But here the array does not exist. Now you can initialize the elements:

Expand|Select|Wrap|Line Numbers
  1. struct item_info
  2. {
  3.       char itemname[15];
  4.       int quantity;
  5.       float retail;
  6.       float wholesale;
  7.       //int quatityonorder;
  8. }item[NOOFITEM] =
  9. {
  10.     {"rice",10,40,30},
  11.     {"sugar",10,40,30},
  12.     {"soap",10,40,30}
  13. };
This is always a question on a job interview.

Expand|Select|Wrap|Line Numbers
  1. int i = 5;
  2.  
i does not exist. This is initialization.

Expand|Select|Wrap|Line Numbers
  1. int i;
  2.     i = 5;
  3.  
i exists. This is assignment.
Aug 26 '14 #2
Hi bro, here NOOFITEM is a macro and will be replaced by 12, sorry I didnt post it! If its 12 then it's an array right?
Aug 26 '14 #3
weaknessforcats
9,208 Expert Mod 8TB
That is correct. It will be an array of NOOFITEM elements.
Aug 26 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: DC | last post by:
Hi, I am programming a windows service and all went well until I needed to use a simple array of chars which I initialize like this: char test = new char {'\x002F', '\x005E'}; Immediatly...
1
by: MAF | last post by:
Is there a quick way to initialize an array with one value for example int IDs = new int; I want all items to be initialized to -1.
2
by: Brett | last post by:
I need an array but don't know how many items will go into it upon declaration. int ArrayIndex = 0; string linkArray = new string{}; for (int i = 0; i < Links.Count; i++) { linkArray =...
15
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 ...
9
by: www.brook | last post by:
hi, I have a class class A { const int m_a; const int m_b; } m_a can be initialized at the constructor A():m_a(2)
22
by: silversurfer2025 | last post by:
Hello everybdy, I am a little confused for the following reason: In my code I used a simple for-loop in order to initialize a 2D-array of floats to zero. Because of efficiency reasons, I changed...
18
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...
10
by: padh.ayo | last post by:
Hi, I have a array structure called people It is properly initialized to 100 array structure elements. Now, I'm reading in from the command line a txt file, and I get them to open correctly. In...
2
by: heng | last post by:
If the data member of a class is an array, how to initialize? I tried the following, but it is wrong. class A { public: int a; A():a({0,0,0}){} };
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

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.