I did some pointer jumping for the first time today and it is quiet interesting. If you don't have an array in e.g. a structure, but consecutive variables, you can just take a pointer and add some bytes to it to jump there, like in these functions:

Code:
BMAP** avViewRtRef (VIEW* v, int index)
{
	if (v == NULL || index < 0 || index >= 4)
		return NULL;
		
	if (index == 0)
		return &(v->bmap);
	else
		return &(v->target1) + (index - 1) * sizeof(BMAP*);
}

BMAP** avMtlSkinRef (MATERIAL* m, int index)
{
	if (m == NULL || index < 0 || index >= 4)
		return NULL;
		
	return &(m->skin1) + index * sizeof(BMAP*);
}



I wanted to have two functions that give me the pointer to the bitmap-pointers of the view render targets (bmap, target1, target2, target3) and the material skins (skin1, skin2, skin3, skin4). In the case of the material, all skins are after another (see struct material in atypes.h), but for views, target1...target3 are not directly in memory after bmap, therefore the if.

Essentially, you just take a pointer adress and add the size of the pointer times the fields you want to jump and voila, you have it.

The (non-efficient) alternative to this would be a switch...case, like this

Code:
BMAP** r = NULL;
	
switch (index)
{
	case 0: r = &(v->bmap); break;
	case 1: r = &(v->target1); break;
	case 2: r = &(v->target2); break;
	case 3: r = &(v->target3); break;
}		

return r;



Hm, I don't know if this is helpful but I feel motivated to share things since that good-bye post of Sid wink

Last edited by HeelX; 05/09/13 19:48.