subtracting pointers...

  • Thread starter Martin Joergensen
  • Start date
M

Martin Joergensen

Hi,

Just a small problem (about taken the previous unsigned value in
a 1D- array and copying it to the current value).

Suppose there's an outer loop over the line shown below, so
unsigned_int_ptr keeps changing, as it is a parameter in the
function called function( &array )....

void function( unsigned *unsigned_int_ptr)
{
...... ...
*unsigned_int_ptr = *(unsigned_int_ptr -
sizeof(*unsigned_int_ptr) );
...... ...
}


Using the debugger I find that:

unsigned_int_ptr = 0x0012fdec
sizeof(*unsigned_int_ptr) = 4
- - - - - - - -
unsigned_int_ptr - sizeof(*unsigned_int_ptr) = 0x0012fddc

Instead of

0x0012fde8, which I want....



How do I get/ why don't I get 0x0012fde8, which must be the
correct value ??? At least this is the value I need for
dereferencing so I get stored the previous value, in the current
position, in the unsigned array....


Best regards / Med venlig hilsen
Martin Jørgensen
 
W

wolf0403

The point is, when doing pointer arithmetic on a pointer of type T*,
the basic unit of adding/subtracting is of sizeof(T). So,
unisigned_int_ptr - sizeof(*unsigned_int_ptr)
really means you're subtracting 4 units, which is, 4 * sizeof(unsigned
int) aka 16bytes off your pointer.
Hope this would be helpful.
Cheers.
Ryan
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

The point is, when doing pointer arithmetic on a pointer of type T*,
the basic unit of adding/subtracting is of sizeof(T). So,
unisigned_int_ptr - sizeof(*unsigned_int_ptr)
really means you're subtracting 4 units, which is, 4 * sizeof(unsigned
int) aka 16bytes off your pointer.
Hope this would be helpful.

Yes, thanks. You're saying I should divide sizeof(*unsigned_int_ptr) by 4:

void function( unsigned *unsigned_int_ptr)
{
...... ...
*unsigned_int_ptr =
*(unsigned_int_ptr -
sizeof(*unsigned_int_ptr)/sizeof(unsigned int) ) );
...... ...
}

Or just:

void function( unsigned *unsigned_int_ptr)
{
...... ...
*unsigned_int_ptr = *(unsigned_int_ptr - 1);
...... ...
}

Which is much easier :)


Best regards / Med venlig hilsen
Martin Jørgensen
 
M

Michael Brennan

Martin said:
Yes, thanks. You're saying I should divide sizeof(*unsigned_int_ptr) by 4:

void function( unsigned *unsigned_int_ptr)
{
...... ...
*unsigned_int_ptr =
*(unsigned_int_ptr -
sizeof(*unsigned_int_ptr)/sizeof(unsigned int) ) );
...... ...
}

Or just:

void function( unsigned *unsigned_int_ptr)
{
...... ...
*unsigned_int_ptr = *(unsigned_int_ptr - 1);
...... ...
}

Which is much easier :)

Yes, the latter one should be used. There's no point in using the first
one since C calculates the sizes automatically for you.

See FAQ 4.4

/Michael
 

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
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top