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.
All C51 mem-space'isms and RTX51'isms cast aside, a review of basic C could be beneficial.
mytable is an array, the name of which produces its (implicit) address. Specifying the address of an (implicit) address could reasonably generate a warning.
Specifying the address of an (implicit) address could reasonably generate a warning.
No. Such a warning would be quite unreasonable. Taking the address of an array to pass it by reference is unusual, but by no means something to warn about. Just because arrays automagically decay into pointers to their first element when necessary doesn't mean they can't have their address taken and used like any other variable.
"Such a warning would be quite unreasonable."
An error would be unreasonable. A warning is allowed, is justified, is not unreasonable, but is not required.
I should add that I don't necessarily agree with the exact warning the OP claimed to get. Something closer to lint's warning (suspicious use of &) would be more appropriate.
is justified,
Pray tell: by what?
mytable is an array, the name of which produces its (implicit) address.
Not in the programming language C it doesn't. The name of an array, just like with any other variable, specifies its value, i.e. a combination of type and address. It'll implicitly convert into into a pointer to the first element in most circumstances. Taking the address of the address is not one of those circumstances, so this:
doesn't even apply. &mytable is not taking the address of an address, it's taking the address of a variable. The type of the resulting term is "pointer to array[8] of struct {...}", and it's a perfectly allowed, correct operation. There's absolutely nothing to warn about.
Taking the address of the address is not one of those circumstances,
Editing mishap. Make that
Taking the address of the array is not one of those circumstances,
"Pray tell: by what?"
An implementation can produce warnings about anything it wants. An implementation producing warnings in suspect situations (e.g., unreferenced variables, unreachable code, 'void' statements, etc.) can justifiably produce a warning about the unnecessary use of '&'.
can justifiably produce a warning about the unnecessary use of '&'.
... except that the use of & is necessary in the case at hand. The code as posted(!) would be incorrect if that '&' was left out. It's correct and doesn't deserve any kind of warning, as-is.
Generally speaking, mytable and &mytable are both perfectly correct arguments to pass to a called function. Only one of them has the correct type, though. Which that is depends on whether the called function's argument type is "pointer to element", "array", or "pointer to array".
As per the declarations actually posted by the OP, &mytable is correct, and mytable is not. The warning is unreasonable for the posted code, so either the posted code is not what the OP actually compiled to get the warning, or the compiler is emitting an unjustifiable warning.
"... a review of basic C could be beneficial."
For me.