473,404 Members | 2,187 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,404 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 3001
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}){} };
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.