473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

subtracting pointers...

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[i] )....

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

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk

May 1 '06 #1
3 2441
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

May 1 '06 #2
wo******@gmail.com wrote:
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

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
May 1 '06 #3
Martin Jørgensen wrote:
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
May 1 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: simpleman | last post by:
Hi, I have an assignment that requires me to subtract two very large unsigned integers using linked list.AFAIK I have to manipulate data as character. But I dont know how to get input into the...
7
by: Old Wolf | last post by:
For the following code: int main(void) { short a, *ptr; ptr = a + 100; ptr -= 0; }
11
by: Laery | last post by:
Hi, I'm currently adding a new module to an old borland C3.1 application (dos). And I need to calculate a date by subtracting the number of days from a given date. I know I could use an...
8
by: Ifollowhim | last post by:
I have a (installation) date in a table that I put in manually. It is the date we want to complete a job. In a form that uses data from that table I want to add a text box that is bound to the...
31
by: Spiro Trikaliotis | last post by:
Hello, I have a question regarding subtracting a pointer from another one. Assume I have two pointers p1 and p2, which both point to a memory area obtained with malloc(). Assume p1 = p2 + some...
2
by: Paulers | last post by:
hello all, how does one go about subtracting seconds from a DateTime object? for example I have a date time of "06/04/2007 10:31" and I need to subtract 12 seconds from that. all help is...
14
by: Krumble Bunk | last post by:
Hi all, I have an elementary question, of which I never grasped, and i'd like to finally put it to rest. I have the following code, which although makes use of the string library, and could...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.