Pointer and integer addition

Joe Herman dzoey at terminus.umd.edu
Fri Mar 29 10:09:44 AEST 1991


Hello, I have a section of code that scans through a block of variable
length records.  The structure looks more or less like this:

struct foo {
	unsigned short recsize;
	unsigned short num;
	char info [24];
	byte flags;
	char filename[1]
};

Where, foo.filename is a placeholder for a variable length string.

After I process one record, I'd like to just increment the pointer to
point to the next record.  What I'd like to do is:

fooptr += fooptr->recsize;

However, the compiler I'm using increments the pointer by
recsize * sizeof (struct foo).   This is probably correct.

Next, I tried

fooptr = (char *) fooptr + fooptr->recsize;

No difference, so I guess it gets the value to multiply the increment from
the lvalue.

Is there a way to do what I want without using a temporary variable
(such as assigning to a char * and then doing the increment) or
converting the pointer to an integral type, doing the addition and
converting back?

				Thanks,
				Joe Herman

dzoey at terminus.umd.edu



-- 
"Everything is wonderful until you know something about it."



More information about the Comp.lang.c mailing list