CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Array arithmetic

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
RossJ



Joined: 25 Aug 2004
Posts: 66

View user's profile Send private message

Array arithmetic
PostPosted: Wed Dec 08, 2004 2:08 am     Reply with quote

My C must be a little rusty (too much Java I guess), but is the following legal in C and are they the same?

Code:
&an_array[4]
an_array + 4

where an_array is an array of int8.

If the array of int8 actually contains an int16 that I want to extract, is the following legal?

Code:
*((int16*) &an_array[4])
*((int16*) (an_array + 4))

The reason I ask is that the CCS compiler doesn't appear to work with the second approach, but the first one is fine. Of course both compile ok Rolling Eyes The problem is that the pointer is miscalculated (the high byte was zero in the case I remember). I suppose it could be a typecast problem. Or is this a known compiler bug? I have seen a few posts about unexpected pointer arithmetic behavior.

I am using PCWH 3.214 with a PIC18F2620.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Dec 08, 2004 9:01 am     Reply with quote

This worked just fine for me

Code:

#device PIC16F877 *=16 ADC=8


int8 an_array[20];

void main()
{

  // This is for an int16 located at byte 4 in the array
  *((int16*) &an_array[4]) =0x1234;
  // This is for an int16 located at byte 4 in the array
  *((int16*) (an_array + 4)) = 0x5678;
  // This is though we declared the array as an int16 and are accessing element 4
  *((int16*)an_array + 4) = 0xABCD;
  while(1);
}
RossJ



Joined: 25 Aug 2004
Posts: 66

View user's profile Send private message

PostPosted: Wed Dec 08, 2004 9:10 pm     Reply with quote

Thanks Mark.

I have done a little further investigation using the following program...

Code:
#device PIC18F2620
#zero_ram

int8 an_array[40];
#locate an_array = 0x420

void main()
{
   int8 offset = 6;
   int8* p;

   // Test 1
   p = an_array + offset + 4;    *p = 0xA1; // written to 0x02A -- ERROR
   p = an_array + (offset + 5);  *p = 0xA2; // written to 0x02B -- ERROR
   p = &an_array[offset + 6];    *p = 0xA3; // written to 0x42C -- OK
   p = an_array + 6 + 7;         *p = 0xA4; // written to 0x42D -- OK

   // Test 2
   *((int16*)an_array + offset + 4) = 0xBBB1;   // written to 0x430/1 -- ERROR
   *((int16*)an_array + (offset + 4)) = 0xBBB2; // written to 0x434/5 -- OK
   *((int16*)an_array + 6 + 4) = 0xBBB3;        // written to 0x434/5 -- OK
   *((int16*)an_array + (6 + 4)) = 0xBBB4;      // written to 0x434/5 -- OK
}

Test1:
As indicated in the comments, p is corrupted in the first 2 examples. It looks like a simple failure to correctly handle the high order byte, rather than a failure to take both offset and the constant into account (since the low order byte of p is correct). It seems that the variable (offset in this case) is triggering the fault, since your test program works fine (I tried it too).

I also ran the above program on a PIC16F877 with the same faulty results. Of course I used PIC16F877 with *=16 and the location of an_array moved to bank 2 (since 0x420 is not available).

Test2:
In addition, I did some experimenting with your third line (the one where the offset is in 16 bit words). I think I found a bug there too.

I'll shoot this off to CCS support.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group