i have function similar following takes integer parameter, , using integer want to access specific variable. of course, can casebash using case/switch statement, or if/else, far dirty , i'm guessing there must form of elegant solution.
static unsigned char frame1[] = {...}; static unsigned char frame2[] = {...}; /*...*/ static unsigned char frame30[] = {...}; int select_frame(int frame_number){ /* how can use frame_number select correct frame variable without case/switch, if @ all? */ }
i feel though there potentially stringstream method (or similar), though i'm not sure. suggestions? i'm happy see solutions in either of c or c++.
edit: feel should note of these arrays have on 500 individual elements in them, , becomes little impractical combine them 1 giant array. if it's possible, i'm looking solution avoids method, though i'm considering it.
if want access 1 of frames based on integer, put frames array:
static unsigned char* frames[30]; frames[0] = (unsigned char*)strdup("first frame's char"); frames[1] = (unsigned char*)strdup("second frame's char");
then can directly , efficiently index frames[i].
in c++, more flexible approach use std::vector<std::string>
(as long numbers contiguous) or std::map<int, std::string>
(good if have numbers gaps, or want insert/erase single elements during runtime.
edit: helper function create string literal source code existing unsigned char arrays (off-the-top-of-my-head, tweaks may needed):
void f(std::ostream& os, unsigned char* p) { os << '"'; ( ; *p; ++p) { if (*p < ' ' || *p >= 127) os << '\\' << std::setw(3) << std::setfill('0') << std::oct << *p; else if (*p == '\\') os << "\\\\"; else os << *p; } os << '"'; }
then call f(frame1); f(frame2); etc...
Comments
Post a Comment