RE: [AVR-Chat] Arrays and pointers in GCC
2009-01-12 by Tim Mitchell
----Original Message----
Show quoted textHide quoted text
From: AVR-Chat@yahoogroups.com [mailto:AVR-Chat@yahoogroups.com] On Behalf Of David VanHorn Sent: 12 January 2009 15:32 To: AVR-Chat@yahoogroups.com Subject: [AVR-Chat] Arrays and pointers in GCC > Is there a reason that I can't use a char as a pointer in > an array of ints? > My ADC experiment has an array of 8 ints to store the > data, and it does grind my gears to use a 16 bit pointer > into an 8 element array. > > In ASM, I'd only use three bits of a byte, saving the > other five for flags, and absolutely preventing indexing > outside the array, but I haven't worked out how to do > that in C yet. > Hi Dave, first it's not a pointer you are using in your code... It's an index into the array. A pointer is an address of something, like you'd use xh:xl for in asm. You need 16 bits for a pointer as it can address the whole of the memory. If you were using a pointer it would go something like this char *pADC_Data = &ADC_Data[0]; //initialise pointer and point the pointer to the address of the bottom of the array *pADC_Data = ADCW; //store ADCW at the location pointed to by the pointer *pADC_Data++; //point to next location You should be able to use a char as the index into your array, how are you defining it and what happens? I share your pain when you look at what the compiler's done, compared to how you can do it in asm. You just have to breathe deeply and let it go. If you're really strapped for execution time, you can write bits of it in asm. Don't even think about trying to use part of a byte as a variable and part for flags. You can do it, but it starts to negate all the reasons for writing in C in the first place. -- Tim Mitchell