Re: Pointers gone mad!
- From: Robby <Robby@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 12 Dec 2007 22:37:00 -0800
Hello everyone,
Yes, its true Igor, I didn't see it with those values. Now I see why I have
to increment A0 before like yor example:
++(*A0);
if (!*A0) { // previous increment overflowed
++(*A1);
if (!*A1) {
++(*A2);
}
}
I also like this one:
void INC_ADDR(int INCBY, int *A2, int *A1, int *A0) {
int carry = (*A0 > 255 - INCBY);
*A0 += INCBY;
if (carry) {
++(*A1);
if (*A1 == 0) {
++(*A2); }}}
But I was meaning to ask you how do we interpret this line:
int carry = (*A0 > 255 - INCBY);
What is going on here, are you doing the subtraction first and if its less
than *A0, it gets assigned to carry? I have never coded this way before. Let
me know I am curious.
Thanks to all of your posts!
--
Best regards
Robert
"Robby" wrote:
Thankyou Igor!.
But what I am trying to say is that after executing the first line of your
code snippet, simultaneously A0 = 1 and A1=256. A0 and A1 were passed in as 0
in this function. So after the first line of your code snipet, A0 should
equal to 1 and A1 should equal to 0 !!!!
++(*A0); //<<<<<After executing this line, *A0=1 and *A1=256 ??????
if (!*A0) { // previous increment overflowed
++(*A1);
if (!*A1) {
++(*A2);
}
}
This must be a problem with the bloody compiler *again*. I tell ya, this
compiler will make me develop an ulcer!
Sincere regards and happy holidays Igor!
--
Best regards
Robert
"Igor Tandetnik" wrote:
Robby <Robby@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
This is a simple questions about pointers! I can't believe I am
asking this. But here it goes. Please note that an integer is 8 bits
long (1 byte)! Please view question which is commented in code below.
=====================================Calling routine!
int spiA0=0,spiA1=0,spiA2=1;
//INCREMENT_ADDRESS BY AMOUNT OF INCREMENTATIONS REQUIRED
INC_ADDR(16,&spiA2, &spiA1,&spiA0);
=========================================Function!
void INC_ADDR( //***INCREMENT_ADDRESS***
int INCBY, //AMOUNT OF INCREMENTATIONS REQUIRED
int *A2, //MSB OF MESSAGE ADDRESS IN PAR FLASH
int *A1, //MIDSB OF MESSAGE ADDRESS IN PAR FLASH
int *A0) //LSB OF MESSAGE ADDRESS IN PAR FLASH
{
int i;
for(i=0;i<INCBY;i++)
{
if((*A0)==255) //Before A0 turns to 0, statement is true
{(*A1)++; //Increment central byte of flash adress
This is wrong. If *A1 is already 255, it will overflow here. Note how
you check A0 before incrementing (correct), but check A1 after
incrementing (wrong).
if((*A1)==255) //Before A1 to 0, statement is true
{ (*A2)++; //Increment MSBYTE of flash adress
}
}
/////////When the next line is executed (While i=0 during 1st
iteration of for loop)
//////// A0 increments to 1. But why does A1 get assigned the value of
256?????
If, as you say, int is 8 bit on your machine, A1 cannot possibly have
the value of 256 (256 doesn't fit into 8 bits).
Personally, I'd do something like this:
++(*A0);
if (!*A0) { // previous increment overflowed
++(*A1);
if (!*A1) {
++(*A2);
}
}
That's your everyday "add with carry" algorithm, just like you were
taught to do with pencil and paper in elementary school.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
- Follow-Ups:
- Re: Pointers gone mad!
- From: Robby
- Re: Pointers gone mad!
- From: Igor Tandetnik
- Re: Pointers gone mad!
- References:
- Pointers gone mad!
- From: Robby
- Re: Pointers gone mad!
- From: Igor Tandetnik
- Re: Pointers gone mad!
- From: Robby
- Pointers gone mad!
- Prev by Date: Re: Style Question
- Next by Date: template function v.s. template class
- Previous by thread: Re: Pointers gone mad!
- Next by thread: Re: Pointers gone mad!
- Index(es):
Relevant Pages
|