Pointer arithmetic

L

lancer6238

Hi,

I have a question on pointer arithmetic. Say I have the following:

uint8_t ext = 1;
struct ex *temp; // sizeof(ex) = 40 bytes

If I were to do

temp += (ext + 2) * 4;

Would I move the pointer "temp" 12 bytes from its original position,
or would it move the pointer 12 * sizeof(ex) = 480 bytes?

Thank you.

Regards,
Rayne
 
B

Beej Jorgensen

uint8_t ext = 1;
struct ex *temp; // sizeof(ex) = 40 bytes

temp += (ext + 2) * 4;

Would I move the pointer "temp" 12 bytes from its original position,
or would it move the pointer 12 * sizeof(ex) = 480 bytes?

"12 * sizeof(struct ex)" bytes. But its only guaranteed to happen if temp
points at an object in an array to begin with, and adding 12 to it
results in a pointer into the same array (or one element past the end),
I believe.

C99 6.5.2.1p2, 6.5.6p7-8

-Beej
 
R

Rayne

Beej Jorgensen said:



Your belief is correct.

I omitted to mention the one-past-the-end in my own reply, which was
remiss of me. As you rightly say, pointing there is just fine. It
is perhaps as well to add that *dereferencing* a pointer that is
one-past-the-end is not. Furthermore, one-before-the-beginning is
/not/ fine, not even just for pointing.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Forged article? Seehttp://www.cpax.org.uk/prg/usenet/comp.lang.c/msgauth.php
"Usenet is a strange place" - dmr 29 July 1999

Thanks for the replies. What if I want to just move 12 bytes past the
current location?
 
R

Rayne

Thanks for the replies. What if I want to just move 12 bytes past the
current location?

I just figured out that to only move 12 bytes past the current
location, I could do

uint8_t *temp2;
temp2 += 12;
temp = (struct ex *)temp2;

Is this the only way to do it?
 
V

Vlad Dogaru

I just figured out that to only move 12 bytes past the current
location, I could do

uint8_t *temp2;
temp2 += 12;
temp = (struct ex *)temp2;

Is this the only way to do it?

Using unsigned char instead of uint8_t would be more portable.
inttypes.h wasn't introduced until C99.

Vlad
 
F

Fred

I just figured out that to only move 12 bytes past the current
location, I could do

uint8_t *temp2;
temp2 += 12;
temp = (struct ex *)temp2;

Is this the only way to do it?


given:
struct ex var;
struct ex *temp = &var;
unsigned char *c =(unsigned char *)temp;
c += 12;

Then c indeed will point 12 bytes into temp.
However if you then say:

struct ex *temp2 =(struct ex *)c;

then it is likely that BAD things will happen.
If sizeof(var) != 12, temp2 will quite likely
be badly mis-aligned. Consider:
struct ex{
double x;
double y;
double z;
} ex;

What do you think will happen now if you
refer to temp2->y ?
 
R

Richard Bos

Rayne said:
I just figured out that to only move 12 bytes past the current
location, I could do

uint8_t *temp2;
temp2 += 12;
temp = (struct ex *)temp2;

Is this the only way to do it?

Alternatively, you could forgo the intermediate temporary, and do

temp = (struct ex *) ( ((char *)temp) + 12 );

(Note that a char is always the smallest type, so you don't need a
uint8_t, and I'd advise against using it unless the 8-bittedness is more
important than the smallest-typehood.)

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top