Questions tagged [struct]

A keyword in various programming languages whose syntax is similar to or derived from C (C++, C#, Swift, Go, Rust, etc.). Use a specific programming language tag to tag questions involving use of a `struct` as syntax and semantics can be language dependent. Keyword defines or declares a data type composed of other data types. Each member of a struct has its own area of memory (as opposed to a `union` whose members share a single area of memory).

A struct consists of a sequence of field names and their types (struct members), for example:

struct s {
    int   *i;                // pointer to an int
    char  *s;                // pointer to a char
    double d;                // a double
    int (*pFunc)(char *, int);  // pointer to a function
};

A struct can also contain bit fields to allow bit-level memory addressing:

struct bits {
    unsigned int b1 : 1;
    unsigned int b2 : 1;
    unsigned int b3 : 1;
    unsigned int b4 : 1;
    unsigned int b5 : 1;
    unsigned int b6 : 1;
    unsigned int b7 : 1;
    unsigned int b8 : 1;
};

Each member of a struct has its own area of memory as opposed to a union in which the members share the same area of memory.

The syntax for defining/declaring a struct as well as what it is possible to include in a struct definition/declaration varies between the various C style languages that use the keyword (e.g. member functions not allowed in C but are in C++ though both allow a pointer to a function).

The syntax for specifying and using a struct to define/declare variables may vary slightly between various C style programming languages ( s myVar; versus struct s myVar;)

Dynamic languages generally use some form of associative array in place of structs. The Pascal family of languages refer to these date types as records.

References

  1. Struct in Swift
  2. Struct in C#
  3. Struct in C++
  4. Struct in C
  5. Struct in Go
  6. Struct in Rust
  7. Classes and structs on MSDN
  8. Struct in Python
30239 questions
133
votes
4 answers

Init array of structs in Go

I'm newbie in Go. This issue is driving me nuts. How do you init array of structs in Go? type opt struct { shortnm char longnm, help string needArg bool } const basename_opts []opt { opt { shortnm: 'a', …
daniel.widyanto
  • 1,609
  • 2
  • 13
  • 9
133
votes
7 answers

C - function inside struct

Im trying to assign a function inside a struct, so far I have this code: typedef struct client_t client_t, *pno; struct client_t { pid_t pid; char password[TAM_MAX]; // -> 50 chars pno next; pno AddClient() { /*…
xRed
  • 1,895
  • 6
  • 20
  • 36
131
votes
8 answers

No == operator found while comparing structs in C++

Comparing two instances of the following struct, I receive an error: struct MyStruct1 { MyStruct1(const MyStruct2 &_my_struct_2, const int _an_int = -1) : my_struct_2(_my_struct_2), an_int(_an_int) {} std::string…
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
130
votes
8 answers

Swift and mutating struct

There is something that I don't entirely understand when it comes to mutating value types in Swift. As the "The Swift Programming Language" iBook states: By default, the properties of a value type cannot be modified from within its instance…
Mike Seghers
  • 1,925
  • 2
  • 16
  • 12
125
votes
5 answers

How can I get the string representation of a struct?

For my application, it does not matter if the string is human readable or not.
abw333
  • 5,571
  • 12
  • 41
  • 48
124
votes
13 answers

C++ Tuple vs Struct

Is there is any difference between using a std::tuple and a data-only struct? typedef std::tuple foo_t; struct bar_t { int id; double value; bool dirty; } From what I have found online, I found that there are two…
Alex Koay
  • 2,915
  • 4
  • 18
  • 16
124
votes
4 answers

Why does struct alignment depend on whether a field type is primitive or user-defined?

In Noda Time v2, we're moving to nanosecond resolution. That means we can no longer use an 8-byte integer to represent the whole range of time we're interested in. That has prompted me to investigate the memory usage of the (many) structs of Noda…
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
123
votes
2 answers

Function for C++ struct

Usually we can define a variable for a C++ struct, as in struct foo { int bar; }; Can we also define functions for a struct? How would we use those functions?
John
  • 4,596
  • 11
  • 37
  • 43
122
votes
5 answers

Is it possible for one struct to extend an existing struct, keeping all the fields?

Using rust 1.2.0 Problem I'm still in the process of learning Rust (coming from a Javascript background) and am trying to figure out if it is possible for one struct StructB to extend an existing struct StructA such that StructB has all the fields…
drebabels
  • 1,992
  • 3
  • 17
  • 13
112
votes
9 answers

Is it safe for structs to implement interfaces?

I seem to remember reading something about how it is bad for structs to implement interfaces in CLR via C#, but I can't seem to find anything about it. Is it bad? Are there unintended consequences of doing so? public interface Foo { Bar GetBar();…
user1228
111
votes
5 answers

Struct constructor: "fields must be fully assigned before control is returned to the caller."

Here is a struct I am trying to write: public struct AttackTraits { public AttackTraits(double probability, int damage, float distance) { Probability = probability; Distance =…
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
110
votes
11 answers

Initialize/reset struct to zero/null

struct x { char a[10]; char b[20]; int i; char *c; char *d[10]; }; I am filling this struct and then using the values. On the next iteration, I want to reset all the fields to 0 or null before I start reusing it. How can I do…
hari
  • 9,439
  • 27
  • 76
  • 110
110
votes
6 answers

Returning two values, Tuple vs 'out' vs 'struct'

Consider a function which returns two values. We can write: // Using out: string MyFunction(string input, out int count) // Using Tuple class: Tuple MyFunction(string input) // Using struct: MyStruct MyFunction(string input) Which…
Xaqron
  • 29,931
  • 42
  • 140
  • 205
108
votes
13 answers

How do I check if a variable is of a certain type (compare two types) in C?

In C (not C++/C#) how do I check if a variable is of a certain type? For example, something like this: double doubleVar; if( typeof(doubleVar) == double ) { printf("doubleVar is of type double!"); } Or more general: How do I compare two types…
con-f-use
  • 3,772
  • 5
  • 39
  • 60
108
votes
7 answers

Why does C++ disallow anonymous structs?

Some C++ compilers permit anonymous unions and structs as an extension to standard C++. It's a bit of syntactic sugar that's occasionally very helpful. What's the rationale that prevents this from being part of the standard? Is there a technical…
Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175