I have been playing with C code in cc65 these days and I have a small issue that right now doesn't hinder my testing but would like to know what I'm doing wrong. I didn't write C code for ages so it may be something that I don't know anymore about.

I will explain with some example so it will be easier to understand. First, what works.

For example, the smallest element, a list of sprite that forms a meta-sprite, ends up in rodata:

The 1st one: heroAnimList is a pointer to a const pointer to unsigned char that is constThe 2nd one: heroAnimList is a const pointer to a const pointer to unsigned char that is const

So in this case the 2nd one should get placed in RODATA.

Read that way it makes it quite easy to understand. I didn't use much C in more than 10 years so I'm either rusty or didn't understand much at the time about const (I was more active around 93 during college but I don't remember much about talking about const with Turbo C at the time).

What a coincidence, I did found that software yesterday when I was trying to find information about arrays of arrays of pointers. I don't know how to use it yet and at first tried to test with "declare", which is like writing English. I will try it with "explain" next time. Thank you for the tip!

Yes, put that way it is logical. I guess I didn't visualize it as 3 arrays at first. I'm so used to language with their own memory management at work that pointers are not something common for me to use. I think I will need to review a little bit on the subject and try to work on that when I'm not exhausted (easier to find bugs this way ^^;;).

A * takes whatever type was on the left, and replaces it with a pointer to that type. That pointer itself is a 2 byte (or 4/8 byte) variable that stores an address, and that stored address can be modified if it's not const. The syntax allows you to put const immediately after the * to specify that it should not be modified.

A [] builds an array of whatever type was on the left. There's no place in the syntax to specify const like there is with *, because there is no pointer here. The array type is like all other variables; it can't be moved in the same way that "int a" can't be moved.

The difference might make some sense with an appeal to the 6502: we can always use absolute addressing to access variables or arrays (with indexing), but pointers always require indirect addressing.

You'll find if you try to assign a [] to another [] it will tell you that it's an invalid lvalue which is what the language spec calls variables, sort of. Array types are a non-modifiable lvalue, so you can't assign directly to them, except as part of the initial definition. (It was historically decided in C that arrays should not be assigned to, because that would really mean to copy the whole array, which is usually inefficient. So, they made arrays a special case, and the spec has to mention that over and over. When structs were added later they decided it was okay to assign to structs, though.. the language might seem inconsistent in this way. The implicit conversion of arrays to pointers makes this slightly more confusing too.)

There's an exception here, though. If the [] type is a parameter to a function, it implicitly behaves like a non-const pointer instead (modifiable lvalue). The language is actually missing a way to specify this as const†. This conversion to a pointer type is forced by the location of the passed array being unknown at compile time. If you need const here, though, you can simply use a const * type instead, as arrays can always implicitly convert to pointers anyway. (So: a parameter [] is really a pointer with a syntax that unfortunately overrides the true array.)

(The importance of const on a parameter is minor, though. The consequences of reassigning it are local to that function; it won't make a difference in DATA vs RODATA etc. like it would with a variable's definition.)

Who is online

Users browsing this forum: No registered users and 2 guests

You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum