Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is special about structs?

People also ask

What are structs used for?

'Struct' keyword is used to create a structure. A structure can contain variables, methods, static constructor, parameterized constructor, operators, indexers, events, and property. A structure can not derive/inherit from any structure or class. A structure can implement any number of interfaces.

Why should I use struct instead of class?

Structs copy the entire value on the assignment, whereas reference types copy the reference on assignment. So, large reference type assignments are cheaper than the value types. Instance field declarations in Struct cannot include variable initializers. But, static fields in Struct can include variable initializers.

Why struct is faster than class?

So based on the above theory we can say that Struct is faster than Class because: To store class, Apple first finds memory in Heap, then maintain the extra field for RETAIN count. Also, store reference of Heap into Stack. So when it comes to access part, it has to process stack and heap.

Why struct is used in C?

Structures (also called structs) are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure. Unlike an array, a structure can contain many different data types (int, float, char, etc.).


A better way of asking the same question would be "what is special about arrays", for it is the arrays that have special handling attached to them, not structs.

The behavior of passing and returning arrays by pointer traces back to the original implementation of C. Arrays "decay" to pointers, causing a good deal of confusion, especially among people new to the language. Structs, on the other hand, behave just like built-in types, such as ints, doubles, etc. This includes any arrays embedded in the struct, except for flexible array members, which are not copied.


First of all, to quote C11, chapter §6.8.6.4, return statement, (emphasis mine)

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

Returning a structure variable is possible (and correct), because, the structure value is returned. This is similar to returning any primitive data type (returning int, for example).

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

So, TL;DR, there is nothing special with structs, the speciality is in arrays.


NOTE:

Quoting C11 again, chapter §6.3.2.1, (my emphasis)

Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. [...]


There isn't anything special about struct types; it's that there's something special about array types that prevents them from being returned from a function directly.

A struct expression is treated like an expression of any other non-array type; it evaluates to the value of the struct. So you can do things like

struct foo { ... };

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

The expression someFoo evaluates to the value of the struct foo object; the contents of the object are returned from the function (even if those contents contain arrays).

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

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