PinaGamer PinaGamer - 1 month ago 8
C Question

Use the same pointer to ask the next value

I'm iterating through an array of structs like this:

typedef struct{
dword_t BaseAddrLow ; //Low 32 Bits of Base Address
dword_t BaseAddrHigh ; //High 32 Bits of Base Address
dword_t LengthLow ; //Low 32 Bits of Length in Bytes
dword_t LengthHigh ; //High 32 Bits of Length in Bytes
dword_t Type ; //Address type of this range.
} descBloque_t ;


to check if there are overlaps in the memory. So I've passed the start adress of my array and the number of entries to the function.

int checOverlaps(descBloque_t * descBloque, int numBloques){

dword_t carry;
int i = 0, flag = 0;
ptrDescBloque_t ptrDescBloque = (ptrDescBloque_t)descBloque;
do{
ptrDescBloque++;
carry = 0;

if(descBloque->BaseAddrLow + descBloque->LengthLow > ++descBloque->BaseAddrLow){
flag = 1;
printStrBIOS("\n\There is overlaps.");
break;
}
else{
i++;
descBloque++;
}

} while(i < numBloques);

return flag;
}


So, I've tried to do this:

if(descBloque->BaseAddrLow + descBloque->LengthLow > descBloque++->BaseAddrLow)
//THERE IS OVERLAPS!! :(


But it seems doesn't work like in arrays when you do something like this:

if(array[i] < array[i+1])
//DO SOMETHING


Or it is possible and I don't know how to do it correctly? My actual solution has been to create an auxiliar pointer that it's situated one position ahead but I think that it is possible with no auxiliar pointer.

EDIT1: In the if statement, I'm asking about the SAME pointer
descBloque
but in different locations. Sorry.

EDIT2: I've also tried with this if statement and it doesn't work.

if(descBloque->BaseAddrLow + descBloque->LengthLow > ++descBloque->BaseAddrLow)


EDIT3: I've added the code of the function at the request of Michael Walz user.

Answer

When postfix increment is used: the operand descBloque++->BaseAddrLow returns the BaseAddrLow of the previous array element but increments the value of descBloque so it now points to the next element. And this updated pointer is actually used in the left side of equation descBloque->BaseAddrLow + descBloque->LengthLow.

When prefix increment is used: the value of the field BaseAddrLow is actually incremented since the prefix increment has lower precedence than member selection (see http://www.difranco.net/compsci/C_Operator_Precedence_Table.htm).

So in both cases you have got not what you wanted. You need to do the following:

if(descBloque->BaseAddrLow + descBloque->LengthLow > (descBloque + 1)->BaseAddrLow)
    //THERE IS OVERLAPS!! :(