B
BartC
Suppose I have these types:
#define byte unsigned char
typedef struct {
int a,b,c,d;
} R; /* assume this is 16 bytes */
And these variables:
int n; /* represents a *byte* offset */
R* p;
I want to be able to do the following:
p += n;
but it doesn't work because n is a byte offset; it's not in terms of R
objects. But the obvious cast:
(byte*)p+=n;
doesn't appear to compile. The workarounds seem to be:
p += n/sizeof(R);
which I don't like. Even though p is always aligned (and n is known to be a
multiple of 16), and I know the divide will cancel out, it seems funny
having to introduce a divide op in the first place. And:
p = (R*)((byte*)p+n);
which is what I'm using but looks very untidy in real code.
In any case, the question remains, *is* there a way to cast an lvalue as
I've shown above?
#define byte unsigned char
typedef struct {
int a,b,c,d;
} R; /* assume this is 16 bytes */
And these variables:
int n; /* represents a *byte* offset */
R* p;
I want to be able to do the following:
p += n;
but it doesn't work because n is a byte offset; it's not in terms of R
objects. But the obvious cast:
(byte*)p+=n;
doesn't appear to compile. The workarounds seem to be:
p += n/sizeof(R);
which I don't like. Even though p is always aligned (and n is known to be a
multiple of 16), and I know the divide will cancel out, it seems funny
having to introduce a divide op in the first place. And:
p = (R*)((byte*)p+n);
which is what I'm using but looks very untidy in real code.
In any case, the question remains, *is* there a way to cast an lvalue as
I've shown above?