--- In AVR-Chat@yahoogroups.com, David VanHorn <microbrix@...> wrote: > ... what was I passing it without the &? You were passing BootString[i], which is the value of the i'th element of the array BootString. Since you've put BootString in program memory, the address of the array is an address in program memory. But, since the C language doesn't know about different address spaces (not even I/O addresses as distinct from memory addresses), it treats the address as if it were in data memory. So, you're getting a value from a piece of memory that doesn't exist. The "&" operator is "take the address of". So, &(BootString[i]) is the address of the i'th element of the array BootString. Now, once again, this will be an address in program memory. But, the function (or maybe macro) you're passing it to understands that and jumps through a hoop or two to make sure the data is retrieved from program memory and not data memory (which is why the function exists). > Casting is something that I'm not up on yet. Casting is taking a variable of one type and treating it as if it were of a different type. You do this in assembler all the time. A strength of C is that the compiler is supposed to protect you from doing it when you don't mean to, as in the above case. An "implicit" cast is one that you don't write but that happens anyway to match up the types. These can be benign (such as treating an 8-bit integer as a 16-bit integer when necessary), wasteful and confusing (such as treating an 8-bit integer as a 16-bit integer when required by the language definition but not strictly necessary), or dangerous and probably wrong, as in your case. With decades of high-level language programming experience behind me, I have a policy of avoiding implicit casts. In other words, I write my casts out in the code. Then, I can see what the compiler is going to do because I'm telling it exactly. These are called "explicit" casts. You might want to consider this. I should also point out (and by the length of my replies, I'm sure you've figured out that I'm between jobs right now) that the parentheses around BootString[i] are not actually necessary. The array post-fix operator [] has higher precedence than the address-of pre-fix operator &, so the array element will be located before the address is taken. Again, experience has led me never to rely on the operator precedence, because why bother to remember it, and instead I explicitly write out the order in which I want operators applied using parentheses. This makes my firmware as verbose as my posts, but at least I can tell what it's going to do. Graham.
Message
Re: WinAVR / GCC question re Stack
2009-03-25 by Graham Davies
Attachments
- No local attachments were found for this message.