Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Thread

RE: [AVR-Chat] Arrays and pointers in GCC

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

Re: [AVR-Chat] Arrays and pointers in GCC

2009-01-12 by David VanHorn

> 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.

Sorry, said pointer, meant index..


> You should be able to use a char as the index into your array, how are
> you defining it and what happens?

I get warnings from the compiler
I really dislike warnings.
The warning tells me that I'm using a char, which of course I already know..
Is there something bad/wrong/inappropriate in using an unsigned char this way?
I could see problems with using a SIGNED char.. That could be entertaining.
(and a fun way to obsfucate code...)

> 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.

And they told me and told me that C is can generate code that's "just
as good" as a human..
I thought they meant some non-random human...

:)

> 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.

Why?

I might be using the wrong terms here, but it seems to me like just
another sort of struct.

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.