Re: Pointers gone mad!
- From: "Igor Tandetnik" <itandetnik@xxxxxxxx>
- Date: Wed, 12 Dec 2007 14:18:58 -0500
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!
- References:
- Pointers gone mad!
- From: Robby
- Pointers gone mad!
- Prev by Date: DLL declarations created by VC++ app wizard
- Next by Date: Re: Pointers gone mad!
- Previous by thread: Pointers gone mad!
- Next by thread: Re: Pointers gone mad!
- Index(es):
Relevant Pages
|
Loading