pointers for speed

Shaun Case shaunc at gold.gvg.tek.com
Tue Feb 26 08:13:26 AEST 1991


I know that when you repeatedly access something like

foo.a.b.c.d[11].value

it is better to declare a (register) pointer, assign it the address
of foo.a.b.c.d[11].value, and do manipulations on that, since it
is faster.

However, I am dealing with a heavily nested structure, such as:

struct a {
	struct aa x;
	struct ab y;
	struct ac z;
	} le_struct;

typedef struct aa {
	struct one something;
	struct two nothing;
	struct three everything;
	struct four whatever;
	};


typedef struct one {
	char foo;
	char foo2;
	short blah;
	short blah2;
	};

And...

I want to access the various members of a.x.something (.foo, .foo2,
.blah, .blah2) quickly.  I have a function that I pass a pointer to 
type struct a, thus:

void do_stuff (struct *a ptr)
{}

and what I want to do is something like

register char *fastptr;

fastptr = &(ptr->x.something) 

and then access fastptr->foo, fastptr->foo2, etc.

I can't seem to get it to work.  Is this possible?
Help would be greatly appreciated, since the structure
I am working with is 6 levels deep at some points, and
I have to access every non-char element within it at least
once.  

( I think that from my foggy earlier days, what I am
trying to do is equivalent to Pascal's WITH feature,
in case what I was describing above isn't clear.  It's
been so long since I've used Pascal, tho, that I can
no longer be sure.  Thankfully.  :-)  )

// Shaun //



More information about the Comp.std.c mailing list