Re: Pointers gone mad!



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



.



Relevant Pages

  • RE: Robby, __int8 limited to: -128 to 127.
    ... And yes an 8bit int cannot hold a value of 256. ... //MIDSB OF MESSAGE ADDRESS IN PAR FLASH ... In order to access the data from my external memory I need a three byte ... and increment the A0 value from 1 to 2. ...
    (microsoft.public.vc.language)
  • Writing Classes
    ... Clock application ... private int hr; //store hours ... public void setTime{ ... //Method to increment the time by one second ...
    (comp.lang.java.programmer)
  • Re: Definition of expression and statement.
    ... The fact that a while loop is recognized ... behavior, e.g., if a is an "int" variable and is initially set to ... The various modifier operators, ... increment and decrement, have TWO uses: ...
    (comp.lang.c)
  • Re: Robby, __int8 limited to: -128 to 127.
    ... int INCBY, //AMOUNT OF INCREMENTATIONS REQUIRED ... //MIDSB OF MESSAGE ADDRESS IN PAR FLASH ... Increment A2. ...
    (microsoft.public.vc.language)
  • Re: Question about ++
    ... I have got a question about the post increment operator. ... It seems int he code above the ++ is completly disregarded ... Java completely evaluates the right hand side of an assignment before ... Do the assignment, setting i to the right hand side result, 1. ...
    (comp.lang.java.programmer)