Number indexing of structs

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Number indexing of structs

Post by mattallen37 »

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?
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Number indexing of structs

Post by afanofosc »

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
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Number indexing of structs

Post by mattallen37 »

Alright, thanks.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
ricardocrl
Posts: 117
Joined: 27 Dec 2010, 19:27

Re: Number indexing of structs

Post by ricardocrl »

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?
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Number indexing of structs

Post by HaWe »

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.
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: Number indexing of structs

Post by muntoo »

For example, a simple implementation (bad coding style, and probably buggy, but I don't care) in C++ would be:

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;
    }
};
Not sure about more complex and flexible implementations; you'll have ask someone else (if you're interested, try on Stack Overflow).
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: Number indexing of structs

Post by spillerrec »

In C++ you can even overload the [] operator so you actually could call C[1] and get the "a" member.

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";
}
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...
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests