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.
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?
Asbj.S.
View all questions in Keil forum