What is special about structures?

I know that in C we cannot return an array from a function, but a pointer to an array. But I want to know what is special about structs , which makes them return functions, even if they contain arrays.

Why does struct packaging make the following program valid?

 #include <stdio.h> struct data { char buf[256]; }; struct data Foo(const char *buf); int main(void) { struct data obj; obj = Foo("This is a sentence."); printf("%s\n", obj.buf); return 0; } struct data Foo(const char *buf) { struct data X; strcpy(X.buf, buf); return X; } 
+81
c arrays struct return return-value
Apr 26 '16 at 17:50
source share
4 answers

The best way to ask the same question would be "what is especially important in arrays", since these include arrays to which special processing is attached, and not struct s.

The behavior of the passed and returned arrays by pointer returns to the original implementation of C. Arrays "decay" into pointers, which causes a lot of confusion, especially among people new to the language. On the other hand, structures are constructed in the same way as built-in types such as int s, double s, etc. This includes any arrays built into the struct , with the exception of flexible array elements that are not copied.

+100
Apr 26 '16 at 17:58
source share

First of all, to quote C11 , chapter ยง6.8.6.4, return , (underline mine)

If a return is executed with an expression, the value of the expression is returned to the caller as the value of the function call expression.

The return of a structural variable is possible (and correct) because the structural value is returned. This is similar to returning any primitive data type (e.g. returning int ).

On the other hand, if you return an array using return <array_name> , it essentially returns the address of the first element of the array NOTE that becomes invalid in the caller if the array was local to the called functions. Thus, returning an array in this way is not possible.

So TL; DR , there is nothing special about struct s, the specialty is in arrays.




Note:

Quote C11 again, chapter ยง6.3.2.1 (my emphasis)

_Alignof it is an operand of the sizeof operator, the _Alignof operator _Alignof or the unary & or string literal used to initialize the array, an expression that has a type '' of type is converted to an expression with a type pointer '' to dial such points to the source element of the object array and is not an lvalue value. [...]

+38
Apr 26 '16 at 17:53 on
source share

There is nothing special about struct ; this is something special about array types that don't allow them to return from a function directly.

A struct expression is treated as an expression of any other type without an array; it evaluates the value of struct . This way you can do things like

 struct foo { ... }; struct foo func( void ) { struct foo someFoo; ... return someFoo; } 

The expression someFoo is evaluated by the value of the struct foo object; the contents of the object are returned from the function (even if the contents contain arrays).

An array expression is handled differently; if it is not an operand of the sizeof or unary & operators, or if it is not a string literal used to initialize another array in the declaration, the expression is converted ("decays") from an array of type " T " to "pointer to T ", and the value of the expression is the address of the first element.

Thus, you cannot return an array by value from a function, because any reference to an array expression is automatically converted to a pointer value.

+11
Apr 26 '16 at 18:00
source share

Structures have public data members, so if the structure has access to the data in the main, but not in the case of a class, it is possible. Thus, the wrapper of the structure is valid.

-5
May 04 '16 at 14:10
source share



All Articles