Is there a way to number index the elements(?) of a struct? I like being able to use multiple types of variables (e.g. strings and bytes), but is there a way to address a specific element, numerically?
If you put one struct in another, can you number access both layers as a more dim array?
Number indexing of structs
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
Number indexing of structs
Matt
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
Re: Number indexing of structs
You cannot do that at the moment. The problem is that would require firmware support for a runtime offset to a variable number.
John Hansen
John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
http://bricxcc.sourceforge.net/
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
Re: Number indexing of structs
Alright, thanks.
Matt
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
-
- Posts: 117
- Joined: 27 Dec 2010, 19:27
Re: Number indexing of structs
But does this make sense, conceptually? I mean, in C or C-like languages at least, I don't remember to have this possibility, if I understood what mattallen said.
You mean using the elements of a structure as if they were in an array?
You mean using the elements of a structure as if they were in an array?
Re: Number indexing of structs
in C you could use a pointer (to the address of the first element of a struct) and inc the pointer value by the value of the size of the following struct element.
Thats not exactly what matt wants, but its sort of "counting forward" the struct elements from one to the next.
Thats not exactly what matt wants, but its sort of "counting forward" the struct elements from one to the next.
Re: Number indexing of structs
For example, a simple implementation (bad coding style, and probably buggy, but I don't care) in C++ would be:
Not sure about more complex and flexible implementations; you'll have ask someone else (if you're interested, try on Stack Overflow).
Code: Select all
struct C
{
int a, b, c;
size_t s = {0, sizeof(a), sizeof(b), sizeof(c)};
void* index(int i)
{
void* r = this;
while(i--)
r += s[i];
return r;
}
};
Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE
Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
-
- Posts: 358
- Joined: 01 Oct 2010, 06:37
- Location: Denmark
- Contact:
Re: Number indexing of structs
In C++ you can even overload the [] operator so you actually could call C[1] and get the "a" member.
Notice the temp variable which is needed because the struct might be padded when it have members of different types. In those cases a simple sizeof implementation would fail. (The pointer arithmetics might also fail on 64-bit, not sure and too lazy to fix or look it up.)
Casting a void pointer is also dangerous, so making a proper implementation would require you to make a wrapper class around the pointer which also stores type information so you can safely cast it. (The firmware doesn't have anything similar to a void pointer so you can't do any of this when the members varies. A variable which could hold multiple types would be rather useful though when we don't have overloading.)
I really want to know what you need this kind of behavior for, as I can only see disadvantages and no use-cases...
Code: Select all
#include <cstring>
struct C{
private:
static const size_t s[];
public:
int a,b,c;
void* operator[]( int i ){ return this + s[i]; }
} temp;
const size_t C::s[] = { (unsigned)&temp.a - (unsigned)&temp, (unsigned)&temp.b - (unsigned)&temp, (unsigned)&temp.c - (unsigned)&temp };
#include <iostream>
int main(){
C c;
*(int*)c[0] = 5;
*(int*)c[1] = 6;
*(int*)c[2] = 2;
std::cout << "a: " << *(int*)c[0] << "\n";
std::cout << "b: " << *(int*)c[1] << "\n";
std::cout << "c: " << *(int*)c[2] << "\n";
}
Casting a void pointer is also dangerous, so making a proper implementation would require you to make a wrapper class around the pointer which also stores type information so you can safely cast it. (The firmware doesn't have anything similar to a void pointer so you can't do any of this when the members varies. A variable which could hold multiple types would be rather useful though when we don't have overloading.)
I really want to know what you need this kind of behavior for, as I can only see disadvantages and no use-cases...
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
Who is online
Users browsing this forum: No registered users and 0 guests