We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I have defined a type for objects we use. The details of the type should not be relevant for the users of the type, so I am "hiding" it behind a typedef. As it happens, this type is currently implemented as an array. This is problematic when we want to pass around pointers to variables of this type.
Example:
#define OBJECT_SIZE 5 typedef char object_t[OBJECT_SIZE]; // A type that happens to be an array int main( void ) { object_t my_object; object_t * p_object; p_object = my_object; // Compiler warning - pointer to different objects // (Correct warning - array decays to pointer to char. ) p_object = &my_object; // Compiler warning - pointer to different objects // Why this warning? // According to knowledgeable people, this should be // OK with respect to the C spec return 0; }
So, the question is: how can I obtain the address of the variable in a way that will give me a pointer to the given type? That is, I need a pointer to array[OBJECT_SIZE] of char, not a plain pointer to char.
With kind regards Asbj.S.
"I need a pointer to array[OBJECT_SIZE] of char, not a plain pointer to char."
The 'C' programming language has no such thing; nothing specifically to do with C51 - that just the way the language works.
Are you sure that the 8051 is a good choice for this kind of programming?
The best you can get is something like:
typedef struct { unsigned char my_hidden_data[OBJECT_SIZE]; } magic_type_t;
The little 8051 isn't the best chip in the world for data abstraction, so doing it the computer science way may be the difference between a working and a failed project.
I'm not sure that I understand what you're really trying to achieve here, but you might try Googling terms like "opaque type" and "opaque pointer"...?
Well, at least Lint seems to think there is such a thing, as given the line (in my example)
p_object = my_object
it gives the warning
Error 64: Type mismatch (assignment) (char (*)[5] = char *)
which is perfectly reasonable. The pointer has been declared as a pointer to a type which is an array[5] of chars, while "my_object" decays into a pointer to the base type of the element, i.e. to a pointer to char.
According to presumably knowledgeable people, the variant
p_object = &my_object;
should be correct C and do what I want. But C51 does not seem to agree.
Regarding the 8051: That is the processor in the chip I am using. But we still try to maintain modularity in the code and clean interfaces between the modules.
Thanks for your input, though!
Asbj.S.
Using a struct is probably what we may end up doing. Do you have any idea whether hiding the array in a struct will have any performance costs, or whether the compiler will be able to see that the address of the array is the same as the address to the struct, and optimze it away?
The 'C' programming language has no such thing;
That's quite completely wrong.
Not only does the programming language have such a thing as "pointer to array"; it even works exactly the same as it would for every other "pointer to something".
The thing that will not work for an array is assignment via that pointer, i.e. no
*ptrToThing = *ptrToOtherThing
That is my understanding too. But how do I get the pointer to the array? As seen from my original example (relevant part of it included below) C51 objects when I try to take the address of the array and assign it to a pointer to the type of the array, giving a warning about "pointers to different objects".
typedef char object_t[OBJECT_SIZE]; // A type that happens to be an array int main( void ) { object_t my_object; // An object of the given type object_t * p_object; // And a pointer to an object of the same type p_object = &my_object; // Compiler warning - pointer to different objects }
Try type casting. Let the compiler know exactly what you want to do...
eg: p_object = (object_t*)(&my_object);
-Dave
But how do I get the pointer to the array?
The same way you would do it for any other variable: by using the '&' operator.
C51 objects when I try to take the address of the array and assign it to a pointer to the type of the array
I'm convinced the compiler is quite incorrect there.
Although the language definition doesn't restrict what a compiler may emit diagnostic messages about, this one is pointless and factually wrong.
Try type casting.
I would advise against that. Type casts of pointers are never the right solution, no matter what the problem seems to be.
Actually, so am I now. I have read up on the spec (well not exactly, the K&R2 appendix). And while an array often is converted into a pointer to its base type, pointing to the first element of the array, this conversion does explicitely not take place when the array is the operand of the & operator. Which means that "&array" should give the correct result.
I am discussing the issue with Keil.