Reputation: 39
So for example I have the next code, and would like to return the sensorData structure array.
struct SensorData[] process_data(struct SensorData sensorData[]){
sensorData[0].LED_1 = 1+1;
sensorData[0].LED_2 = 2+2;
return sensorData; }
For me it doesnt even compile.
Upvotes: 1
Views: 2031
Reputation: 12698
First, you have declared an unspecified array type (as no number of element is specified between the brackets) as the return value of the function. This works with parameters (because the compiler converts the array references to pointers, and the actual declaration is, indeed, struct SensorData *
. This can be done with parameters, but not with return values, that have to be completely specified.
Second, you cannot return an array. The array identifier, by itself, doesn't name the array in C, but is just a reference (a pointer) to the first element. So there's no way to return an array as can be done with a struct
, where when you use the variable name, you are referring to the whole variable.
In this sense, you are allowed to do:
struct SensorData {
double values[100]; /* you have to be complete here */
};
and then:
struct SensorData myFunction(struct SensorData data1) {
struct SensorData value_to_return;
...
return value_to_return;
}
but before doing that, you have to think carefully, as you are passing a whole structure by value in the parameter data1
, so space must be allocated on each call to myFunction
to store a copy of the variable you are going to pass (remember all variables are called by value), and space must be reserved (normally on thread stack) for return value, that will be filled by the function, and then copied back to the final destination. You will get a severe efficiency penalty.
When you make an assignment between two struct
objects, you get both variables assigned, if you say
struct SensorData a, b;
and later
a = b;
that's correct... all fields of a
variable are updated with the same ones from variable b
. But that's not the case with arrays.
struct SensorData a[100], b[100];
even both being the same type (as shown above)
a = b; /* this is incorrect */
because a
is not an assignable object(an lvalue, in C nomenclature)... it's only the address of the array specified as a pointer to the first array element.
The thing is that you have a way of doing what you want, just wrap your arrays inside structures, and then you'll manage variables as if they where units.
struct SensorDataWrapper {
struct SensorData array[100];
} a, b;
then you can say
a = b;
and both structures will be copied from b
to a
without any problem. You'll be able to pass them by value (not recommended) and to return them as return values from functions (also not recommended)
Types in C are static, which mean that the compiler defines them strictly at compile time... This makes impossible to develop a type that changes at runtime, you have to use dynamic structures for that. Passing a pointer and the compatibility between pointers and arrays to a function is the only way to pass variable length arrays to a function, and when you declare a parameter as an array pointer with unspecified number of elements, you are declaring a different kind of object (indeed a pointer and not an array)
Upvotes: 1
Reputation: 3688
If your dead set on copying your array to the stack so it can be copied back as a return value you can do something like this with a structure but it's terribly inefficient and seems quite pointless.
struct {
struct SensorData[100];
} sensorArray;
struct SensorArray process_data(struct SensorData sensorData[]) {
sensorArray array;
array.sensorData[0].LED_1 = 1+1;
array.sensorData[0].LED_2 = 2+2;
return array;
}
main() {
sensorArray copy;
copy = process_data(copy.sensorData);
}
Upvotes: 2
Reputation: 499
It doesn't compile because C doesn't allow returning an array, but it will allow you returning a pointer.
Upvotes: 4