I have a function (attempt_volume_exchange_move
) within which I define a structure (status
). I want to pass status
variables to a function I call within attempt_volume_exchange_move
(store_box_properties
). When trying to write the code for store_box_properties
, I get the following warning message on CLion:
Declaration of 'struct status' will not be visible outside of this function.
The code that illustrates my problem follows:
struct boxes {
// Member 1
// Member 2
// Member 3
// ...
// Member N
} box_dense, box_rare;
void attempt_volume_exchange_move(void) {
struct status {
struct boxes box_dense, box_rare;
} original, new;
store_box_properties(&original.box_dense);
store_box_properties(&original.box_rare);
}
void store_box_properties(struct status *status_pointer) { // Line where the warning message is given
// Code for store_box_properties
}
How can I address this problem? I thought about making status
a global structure; however that seems wasteful as the definition of status
only makes sense within the scope of attempt_volume_exchange_move
. I am trying to address this in the most "correct"/"appropriate"/"elegant" way possible.
Thanks beforehand for any insight!
CodePudding user response:
At present you have two different declarations of structures with the same name. The first one is within the function attempt_volume_exchange_move
void attempt_volume_exchange_move(void) {
struct status {
struct boxes box_dense, box_rare;
} original, new;
//...
that is invisible outside the function block scope.
The second one in the parameter list of the function store_box_properties
void store_box_properties(struct status *status_pointer)
that also is invisible outside the function.
As the both functions must refer to the same structure then the structure should be declared and defined outside the functions.
You could do what you are trying to do by defining in each function local structures that are compatible and pass an object of the structure type from one function to another function through a pointer to void. And in the second function you could cast the pointer to a pointer of the structure type declared in this function.
But this makes the code unclear and difficult to modify.
Pay attention to that in the provided by you code the argument expressions used in these calls
store_box_properties(&original.box_dense);
store_box_properties(&original.box_rare);
are not of the type struct status *
. They are of the type struct boxes *
.
So why not to declare the function store_box_properties
like
void store_box_properties(struct boxes *status_pointer)
CodePudding user response:
Your struct status
type
void attempt_volume_exchange_move(void) {
struct status {
struct boxes box_dense, box_rare;
} ...
is function-local and if you pass pointers to it to another function, then the target content cannot be treated as struct status
in that function.
C doesn't not allow you to access a block-local definition from a different function. If you redefine the local struct status
elsewhere in the translation unit, the new definition will not be compatible with the block-local one. (And although you could utilize the http://port70.net/~nsz/c/c11/n1570.html#6.2.7 loophole that allows you to access it in another translation unit if you provide a practically identical (same member names, types, alignments and same tags) struct/union definition there, I wouldn't recommend that approach because it's not compatible with arbitrarily includable header files).
If you're just trying to pass pointers to original.box_dense
, original.box_rare
, which are of global type struct boxes
, then that you can easily do:
https://gcc.godbolt.org/z/fc33xo7bP
struct boxes {
// Member 1
// Member 2
// Member 3
// ...
// Member N
} box_dense, box_rare;
void store_box_properties(struct boxes *status_pointer);
void attempt_volume_exchange_move(void) {
struct status {
struct boxes box_dense, box_rare;
} original, new;
store_box_properties(&original.box_dense);
store_box_properties(&original.box_rare);
}
void store_box_properties(struct boxes *status_pointer) { // Line where the warning message is given
// Code for store_box_properties
}
Perhaps you meant to type the argument of store_box_properties
as struct boxes *
rather than struct status *
?