Re: Pointers gone mad!



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


.



Relevant Pages

  • Re: Pointers gone mad!
    ... Sincere regards and happy holidays Igor! ... int INCBY, //AMOUNT OF INCREMENTATIONS REQUIRED ... //MIDSB OF MESSAGE ADDRESS IN PAR FLASH ...
    (microsoft.public.vc.language)
  • Re: Pointers gone mad!
    ... You mean you're storing an 8-bit quantity in an int, ... //MIDSB OF MESSAGE ADDRESS IN PAR FLASH ... I reformatted your loop so I could read it more easily and changed all the ... postfix to prefix, because it's possible to do so here without changing the ...
    (microsoft.public.vc.language)
  • Re: Pointers gone mad!
    ... This is why I was confused when you used an int! ... Best regards ... //MIDSB OF MESSAGE ADDRESS IN PAR FLASH ... if {// previous increment overflowed ...
    (microsoft.public.vc.language)
  • Pointers gone mad!
    ... This is a simple questions about pointers! ... int INCBY, //AMOUNT OF INCREMENTATIONS REQUIRED ... //MIDSB OF MESSAGE ADDRESS IN PAR FLASH ... Best regards ...
    (microsoft.public.vc.language)
  • [PATCH] I2C: Coding style cleanups to via686a
    ... (These conversions were contributed by Jonathan Teh Soon Yew ... int inNum) ... smooth function fit to the data will allow us to get better precision. ... - VIA register values 0-255. ...
    (Linux-Kernel)

Loading