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 2996
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: 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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
| |