Hi, guys, I somewhere read "You cannot initialize a structure like that at run time."
example: - struct item_info
-
{
-
char itemname[15];
-
int quantity;
-
float retail;
-
float wholesale;
-
}item[NOOFITEM];
-
int main()
-
{
-
item[0]={"rice",10,40,30};
-
item[1]={"sugar",10,40,30};
-
item[2]={"soap",10,40,30};
-
}
But if you want to assign values at run time then you have to do it manually like: - strcpy(item[0].itemname, "rice");
-
item[0].quantity = 10;
-
item[0].retail = 40;
-
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! - struct item_info
-
{
-
char itemname[15];
-
int quantity;
-
float retail;
-
float wholesale;
-
//int quatityonorder;
-
}item[NOOFITEM] =
-
{
-
{"rice",10,40,30},
-
{"sugar",10,40,30},
-
{"soap",10,40,30}
-
};
That is correct. It will be an array of NOOFITEM elements.
3 3001
If the array exists, then you have to assign values individually. - item_info array[15];
-
//At this point the array exists so you need assignment
-
strcpy(array[0].itemname,"rice");
But here the array does not exist. Now you can initialize the elements: - struct item_info
-
{
-
char itemname[15];
-
int quantity;
-
float retail;
-
float wholesale;
-
//int quatityonorder;
-
}item[NOOFITEM] =
-
{
-
{"rice",10,40,30},
-
{"sugar",10,40,30},
-
{"soap",10,40,30}
-
};
This is always a question on a job interview.
i does not exist. This is initialization.
i exists. This is assignment.
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?
That is correct. It will be an array of NOOFITEM elements.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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.
|
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 =...
|
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
...
|
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)
|
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...
|
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...
|
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...
|
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}){}
};
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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,...
|
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...
| |