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
59
votes
6 answers

Why can't we initialize members inside a structure?

Why can't we initialize members inside a structure ? example: struct s { int i = 10; };
Santhosh
  • 881
  • 2
  • 7
  • 6
58
votes
6 answers

C# struct new StructType() vs default(StructType)

Say I have a struct public struct Foo { ... } Is there any difference between Foo foo = new Foo(); and Foo foo = default(Foo); ?
Bala R
  • 107,317
  • 23
  • 199
  • 210
58
votes
6 answers

Are Structs always stack allocated or sometimes heap allocated?

I was of the impression that in C#, struct elements are allocated on the stack and thus disappear when returning from a method in which they were created. But what happens if I place the struct-values in a list and return that? The elements…
Carlo V. Dango
  • 13,322
  • 16
  • 71
  • 114
58
votes
4 answers

How to automatically create an initializer for a Swift class?

UPDATE: Use structs and not classes. Struct is better in many ways has got an initializer of its own. This is my model class. Is it possible to create the init method automatically? Everytime I have to initialize all the variables one by one and it…
Bhavuk Jain
  • 2,167
  • 1
  • 15
  • 23
58
votes
8 answers

Structure of a C++ Object in Memory Vs a Struct

If I have a class as follows class Example_Class { private: int x; int y; public: Example_Class() { x = 8; y = 9; } ~Example_Class() { }…
hhafez
  • 38,949
  • 39
  • 113
  • 143
58
votes
9 answers

Casting one C structure into another

I have two identical (but differently named) C structures: typedef struct { double x; double y; double z; } CMAcceleration; typedef struct { double x; double y; double z; } Vector3d; Now I want to assign a…
Ortwin Gentz
  • 52,648
  • 24
  • 135
  • 213
58
votes
4 answers

Structs, Interfaces and Boxing

Possible Duplicate: Is it safe for structs to implement interfaces? Take this code: interface ISomeInterface { public int SomeProperty { get; } } struct SomeStruct : ISomeInterface { int someValue; public int SomeProperty { get {…
Sekhat
  • 4,435
  • 6
  • 43
  • 50
58
votes
8 answers

Iterating over a struct in C++

I have a structure typedef struct A { int a; int b; char * c; }aA; I want to iterate over each an every member of the structure and print its value. Something like: void print_struct_value(struct *A) { for each member of struct A …
msd_2
  • 1,117
  • 3
  • 16
  • 22
57
votes
6 answers

how to assign multiple values into a struct at once?

I can do this on initialization for a struct Foo: Foo foo = {bunch, of, things, initialized}; but, I can't do this: Foo foo; foo = {bunch, of, things, initialized}; So, two questions: Why can't I do the latter, is the former a special…
jim
  • 661
  • 1
  • 6
  • 8
57
votes
9 answers

In a structure, is it legal to use one array field to access another one?

As an example, consider the following structure: struct S { int a[4]; int b[4]; } s; Would it be legal to write s.a[6] and expect it to be equal to s.b[2]? Personally, I feel that it must be UB in C++, whereas I'm not sure about C. However, I…
Nikolai
  • 1,499
  • 12
  • 24
57
votes
2 answers

note: 'person::person()' is implicitly deleted because the default definition would be ill-formed

I'm working on an example program to help me learn structs in C++. Here's my code: #include #include #include using namespace std; int nextPersonID = 0; int nextAddressID = 0; struct date { int day; int…
Dessa Simpson
  • 1,232
  • 1
  • 15
  • 30
55
votes
1 answer

initialize string pointer in struct

Go Newbie question: I am trying to init the following struct, with a default value. I know that it works if "Uri" is a string and not pointer to a string (*string). But i need this pointer for comparing two instances of the struct, where Uri would…
Mandragor
  • 4,684
  • 7
  • 25
  • 40
55
votes
2 answers

How to initialize member-struct in initializer list of C++ class?

I have the following class definitions in c++: struct Foo { int x; char array[24]; short* y; }; class Bar { Bar(); int x; Foo foo; }; and would like to initialize the "foo" struct (with all its members) to zero in the initializer of…
Jan Rüegg
  • 9,587
  • 8
  • 63
  • 105
55
votes
4 answers

Elixir convert struct to map

I'm trying to convert a struct to a map to be able to clean all the nil values I'm currently using this code case Nadia.get_updates do {:ok, results} -> Map.from_struct(results) |> Enum.filter(fn {_, v} -> v != nil end) |>…
user2070502
  • 603
  • 1
  • 5
  • 8
55
votes
4 answers

Golang basics struct and new() keyword

I was learning golang, and as I was going through the chapter that describes Structures, I came across different ways to initialize structures. p1 := passport{} var p2 passport p3 := passport{ Photo: make([]byte, 0, 0), Name: "Scott", …
scott
  • 1,557
  • 3
  • 15
  • 31