-
Quick Fill "struct"s
Hullo :)
Let's say I Initialise a struct with:
Code:
struct MyStruct
{
FLOAT A, B, C;
};
Now, when I initialise a variable, I can do:
Code:
MyStruct MyVariable[]=
{
{1, 2, 3}
{2, 6, 2}
//And so forth
};
What if though, I want to add another data set to the array? Right now MyVariable[0] and MyVariable[1] have info, but MyVariable[2] does not.
Do I have to do each component seperately?
I.e.
Code:
MyVariable[2].A = 4;
MyVariable[2].B = 1;
MyVariable[2].C = 0;
I've tried:
Code:
MyVariable[2]=
{
{4, 1, 0}
};
//Which gives me many errors about the {} brackets.
//I also tried:
MyVariable[2] = (4, 1, 0);
//Which tells me that the Binary "=" sign can't assign the floats (or something to that effect)
//I've also tried other combinations, but none have worked
So, I guess my question is, do I have to settle for that first one? Doing each element of the structure individually?
-
Either you do it individually, or..
Code:
MyStruct blah = { 0, 1, 2 } ;
memcpy(&MyVariable[2],&blah,sizeof(MyStruct)) ;
-
Code:
MyStruct MyVariable[]=
{
{1, 2 }, // MyVariable[0].C = 0
{2, }, // MyVariable[1].B = 0, MyVariable[1].C = 0
//And so forth
};
If you have a partially complete initialiser, the remaining elements are filled with zeros of the appropriate type (0 for ints, 0.0 for floats, (T*)0 for pointers of type T).
-
Along the same lines, would I be able to use the memcpy method to copy over an array of "MyStruct"s INTO another array?
I.e. Given:
Code:
MyStruct Temp[3];
MyStruct All[];
I'm looking for a faster way to accomplish:
Code:
memcpy(&All[1],&Temp[0],sizeof(MyStruct));
memcpy(&All[2],&Temp[1],sizeof(MyStruct));
memcpy(&All[3],&Temp[2],sizeof(MyStruct));
Is there some way I could choose a starting point in the "All" variable and bring over the entire "Temp" variable at once to fill in a chunk of the "All" variable?
Thanks a ton for the help so far :)
-
try:
Code:
memcpy(&All[1], &Temp[0], sizeof(MyStruct) * number_of_items);
where number_of_items equals the number of elements that you want to copy.
-
>> MyStruct All[];
You can't do that, by the way. Either give it a size or initialize it with the number of cells you want.
-
I can't?........damn. :)
The thing is, I don't know how big I want it to be yet, so I was just planning on adding to it during runtime. And I don't know how much I'll be adding...should I just make it one very massive variable? Cause that seems like a waste of resources....and there's always the problem that, what if I need more than initialised?
-
If you need more than initialized then you can have a pointer to a a datastructer. declared as "
MyStruct * myvariable = calloc(sizeof(MyStruct), numberofelements);"
To add an element do this;
"MyVariable = realloc(MyVariable, numberofelements * sizeof(MyStruct))"
now to access "MyVariable[element].whatever = whatever"
need to resize use realloc again and need to delete use free.
Hope that helps!
-
I would use
Code:
MyStruct * myvariable = (MyStruct *) malloc(sizeof(MyStruct) * numberofelements) ;
instead of your calloc example. malloc doesn't waste cycles setting everything to 0.
-
Thanks for the help guys :) This thread's getting kinda too close to my thread in the other part of the forum, so I'll change the subject. (But thanks again, it's all been very helpful) :)
I have one more question about structures.
If I declare:
MyStruct Structures[]={
{2,5,3},
{1,5,8},
};
(For Instance)
how am I able to compare various Indices to see if they have data yet?
I.e.
if (Structures[3] == NULL)
something to that effect. (That one doesn't work because C++ says I can't do binary comparison).
-
You can't do if (Structures[3] == NULL)
since it isn't a pointer.
The only way to do it is to add a variable telling if Structures[X] is initalized or using an array of pointers to MyStructs