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.
Having some problems working with a struct typedef. In RTX51, one of the system functions takes as an argument a pointer to a struct:
//function prototype signed char os_check_mailboxes (t_rtx_allmbxtab xdata *table); ... /* Type definition for system call os_check_mailboxes */ typedef struct { unsigned char message_cnt; unsigned char read_task_cnt; unsigned char write_task_cnt; } t_rtx_allmbxtab[8];
I've never seen an array-of-structs declared within the typedef before, and couldn't find any literature about it. It's declared like a single struct, and used just like an array of structs:
t_rtx_allmbxtab xdata mytable; if(mytable[x].message.cnt < 2)...
however, I get a warning "C182: pointer to different objects" when trying to use it with the actual system function (exactly as shown in the RTX51 user manual):
os_check_mailboxes(&mytable); //<-- generates warning!
I'm not sure exactly how the array as part of the data type affects this, so i'm stumped. However, that didn't stop me from trying random things to see what DID work. Here's what I came up with; maybe somebody can explain to me why this works:
//declare as an array, effectively making a 2D array //with one dimension being size 1 only t_rtx_allmbxtab xdata mytable[1]; //use as a normal 2D array if(mytable[0][x].message.cnt < 2)... //system function now works fine. no warnings. os_check_mailboxes(&mytable); //using (&mytable[0]) or (&mytable[0][0]) instead will generate 'pointer to different objects' warnings.
To be portable, it would have to have a condition to select the right definition for the particular compiler; eg,
#if defined __C51__ typedef unsigned char uint8_t; typedef signed char int8_t; #elif defined <other-compiler-flag> // suitable typedefs #else #error Unsupported compiler #endif
You just need to change the typedefs for the various types. 8 bits is 8 bits and 16 bits is 16 bits no what compiler your using. Everything depends on the native registor size of the uProcessor. I have code written for an 8-bit processor running on an ARM in 32-bit mode.
"You just need to change the typedefs for the various types."
Exactly - and the #if selects the right one automatically!
"Everything depends on the native register size of the uProcessor."
It also depends on the specific compiler - different compilers for the same target might choose different sizes for some types...
it would have to have a condition to select the right definition for the particular compiler
Actually (as I'm sure Andy knows) you can just have a single header that differs for each particular compiler.
C99 calls it "inttypes.h".
It's probably easier to have one such header per compiler with a compiler-specific implementation than a single header with a long string of #if matching compiler-dependent preprocessor variables.
You mean <stdint.h>, of course.
Actually, I meant inttypes.h, but was simply wrong :)
Inttypes.h includes stdint.h, which is the file that actually defines the integer types. inttypes.h adds some extra definitions that you might not always need, so it would be better to include stdint.h directly.