0

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.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • Not directly. But you’re returning a pointer that was passed in; can’t you return nothing instead? The caller already decided where the data should go. – Ry- Aug 01 '18 at 18:33
  • 1
    If asking about build errors, always include the actual errors you get. Please edit your question to show the errors. Copy-paste (as text) them in full and complete and without any editing. – Some programmer dude Aug 01 '18 at 18:33
  • 2
    And please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask), learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve), and also read [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Aug 01 '18 at 18:34
  • Yes, I know that I could use pointers, maybe for this example it looks pointless... But is it possible to do without pointers? just by return? – Mr Koval Koval Aug 01 '18 at 18:36
  • 1
    Possible duplicate of [C returning array in function](https://stackoverflow.com/questions/16139101/c-returning-array-in-function) – Sneftel Aug 01 '18 at 18:42
  • The code has become a "moving target" after receiving an answer, please don't do that. Rolled back. – Weather Vane Aug 01 '18 at 18:48
  • Thanks, it answered my question. Now I have another one, how to copy an array, in this case, a structure array? – Mr Koval Koval Aug 01 '18 at 18:49
  • If an answer allowed you to fix the code, please "accept" that answer, then ask a new question. Stackoverflow is not a dynamic code editing site. – Weather Vane Aug 01 '18 at 18:50
  • ok, thank you, and sorry, its 1st time... I am a bit confused... – Mr Koval Koval Aug 01 '18 at 18:52

3 Answers3

3

It doesn't compile because C doesn't allow returning an array, but it will allow you returning a pointer.

ad3angel1s
  • 484
  • 1
  • 5
  • 17
  • Hmmm, ok, so it is not possible to return any kind of arrays?! – Mr Koval Koval Aug 01 '18 at 18:40
  • No, it's not possible to return arrays from functions in C: See here for example: https://www.tutorialspoint.com/cprogramming/c_return_arrays_from_function.htm – ad3angel1s Aug 01 '18 at 18:42
  • 1
    When passing the parameters as well, what you're really passing is a struct SensorData *, as the array notation in the parameter list is just syntactic sugar for the pointer notation. – ad3angel1s Aug 01 '18 at 18:45
  • For a 1D array, yes, but for an array with more dimensions the compiler needs to know all but the outermost length. – Weather Vane Aug 01 '18 at 18:53
2

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);
}
cleblanc
  • 3,678
  • 1
  • 13
  • 16
1

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)

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31