View Full Version : quick array question
pbarnes 11-11-2009, 08:55 PM i have a struct and in it is an array
when i call up the struct and add values tot the array, how can i add them all at once?
NOTE::this is for testing purposes,
typedef myStruct
{
float row[4];
};
int main()
{
myStruct foo;
//values : 94, 89, 66, 45
//i can add one by one
foo.row[0] = 94;
//doing
foo.row[0,1,2,3] = 94, 89, 66, 45;
//does not work
//it compiles but if i print one of those values, it is not correct
//how do i do all at once
//if i make an array
float rr[4]={94, 89, 66, 45};
//what would the eqiv be
//my actual struct mas many arrays
//so entering one by one is a long process
}
thx
|
|
Kuroyume0161
11-13-2009, 12:41 AM
Have you tried one of these?
foo.row = { 94, 89, 66, 45 };
foo.row[] = { 94, 89, 66, 45 };
If that doesn't work then there probably isn't going to be a more efficient way than index by index value assignment. C/C++ are not exactly designed for code efficiency. That would be Python, Ruby, Java, and more modern languages.
C/C++ are not exactly designed for code efficiency. That would be Python, Ruby, Java, and more modern languages.
Thats is wrong
You could write a helper function if you want to make the assignment easier, but I do not recommend it. An other good idea might be to have the data loaded from a file.
Kuroyume0161
11-14-2009, 01:52 PM
Thats is wrong
You could write a helper function if you want to make the assignment easier, but I do not recommend it. An other good idea might be to have the data loaded from a file.
???
I've been programming in C for 20 years and C++ for 10. I don't mean COMPILED efficiency. I mean the amount of typing in source to do something.
No, I'm not wrong. Tyvm.
Robert
M3taSpl0it
11-14-2009, 02:50 PM
???
I've been programming in C for 20 years and C++ for 10. I don't mean COMPILED efficiency. I mean the amount of typing in source to do something.
No, I'm not wrong. Tyvm.
Robert
yes you're right , there were syntax problems )
pbarnes
11-14-2009, 03:08 PM
i tried the suggestions, did not work
i take it there is no quick method then
hulud
11-15-2009, 10:22 AM
My 2 cents solution :
const float row[4] = {94, 89, 66, 45};
memcopy (foo.row, row, sizeof(row));
Ok, there is no smart way to do that in C++ :)
Cyril
you can write a function which is takes the array as the first argument and n-elements after that and then make the function do the assigment, so in your code you could do something like this:
myAssignFunction(&myArray, 1, 2, 3, 4, 5, 6)
For it to work with n-elements you have to look into using "..." as the second parameter. I have never tried this myself, but I have seen it in the codebase where I work.
Example from Wikipedia:
#include <stdarg.h> double average(int count, ...) { va_list ap; int j; double tot = 0; va_start(ap, count); //Requires the last fixed parameter (to get the address) for(j=0; j<count; j++) tot+=va_arg(ap, double); //Requires the type to cast to. Increments ap to the next argument. va_end(ap); return tot/count; }
Kuroyume0161: By code efficiency I assumed you meant speed.
Regards,
pbarnes
11-15-2009, 12:03 PM
My 2 cents solution :
const float row[4] = {94, 89, 66, 45};
memcopy (foo.row, row, sizeof(row));
Ok, there is no smart way to do that in C++ :)
Cyril
cheers this worked!
was hoping there was a quicker way, but if this is the way, so be it
cheers to the rest of u guys!
Kuroyume0161
11-15-2009, 04:39 PM
cheers this worked!
was hoping there was a quicker way, but if this is the way, so be it
cheers to the rest of u guys!
That will work but you do need to put some stuff on the stack (the input array filling foo.row). To reduce its existence time, you should make a function where you initialize all of your arrays. Then row and other stack variables will go away as soon as the function exits.
Like this:
void initialize()
{
// these are created on program stack
const float row[4] = {94, 89, 66, 45};
// etc. for other arrays
memcpy (foo.row, row, sizeof(row));
// etc. for other array assignments
// when you exit, row and other local stack variables are freed
}
Kuroyume0161
11-15-2009, 04:46 PM
Kuroyume0161: By code efficiency I assumed you meant speed.
Regards,
Maybe I should have said coding efficiency. Usually this is clumpled together as productivity. From what I've read up on this, Java isn't looking as good as it used to (mainly due to the amount of class and method look up). When I moved from C to C++ I quickly stopped (this is when the C++ standard was really a mess and not well implemented) and moved to Java. For some time, compared to C/C++, Java was very fast in productivity but it has started to become about the same. Python, Ruby are still two high-speed productivity languages. I'm currently learning Python in depth in an attempt to move away from C++ a bit.
hulud
11-15-2009, 08:41 PM
Yeah, I agree with that. Writing less C++ code and working with a higher level language is a good idea. For my part, I like LUA for that, small footprint, fast, easy to integrate with C++.
CGTalk Moderation
11-15-2009, 08:41 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
vBulletin v3.0.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.