-
Load value to struct.
Say I have the following structs:
Code:
struct stA
{
int Num;
stB Other[100];
}One;
struct stB
{
char list[100];
int Num2;
}Two;
then how would I go about putting a string value into the second struct type stB in position 0 in the location list?
This is all I can figure, but when I print it all I get is a whole screem of jiberish:
Code:
char C[100] = "asdf";
One.Two[0].list = C[100];
It just doesnt work.. anyone got an idea? do I have to declare it some how else?? I think there is a way to do it using curly brackets but I couldnt get that to work either.. HELP!
-
But struct stB will have to be above/before struct stA.
Code:
#include <iostream>
using namespace std;
struct stB
{
char list[100];
int Num2;
}Two;
struct stA
{
int Num;
stB Other[100];
}One;
int main()
{
cin >> One.Other[0].list;
cout << One.Other[0].list << endl;
return 0;
}
-
Ok, I would image this would be the correct way to put the string into the type stB struct, but I get the following error:
Code:
One.Two[0] = {"asdf",1};
I get the error
Code:
: error C2059: syntax error : '{'
: error C2143: syntax error : missing ';' before '{'
: error C2143: syntax error : missing ';' before '}'
EDIT: But I cant move them do to my problem.. is there any way of getting that string in that location?
-
You are getting confused on what it an array and what is not.
"Two" is a struct of type stB, there is only one "Two", but that "Two" has an array in it called list.
Two.list[i] is valid;
There is also only one "One" of struct type atA, but that one "One" has an array of type stB.
So "One" has an array size 100 of type stB; which in turn has an array of size 100 in it.
So you now have 100 things, and each can hold 100 things.
If it was One[2]; you would now have an array of stA, in which each one would have an array of size 100 of stB which in each one of those would have a char array of size 100.
And that is very confusing and why we use more descriptive names for our variables and never use something like “one” or “two”!!!