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
55
votes
5 answers

Initializing an Array of Structs in C#

How can I initialize a const / static array of structs as clearly as possible? class SomeClass { struct MyStruct { public string label; public int id; }; const MyStruct[] MyArray = { {"a", 1} …
Adam Tegen
  • 25,378
  • 33
  • 125
  • 153
55
votes
8 answers

How to include a dynamic array INSIDE a struct in C?

I have looked around but have been unable to find a solution to what must be a well asked question. Here is the code I have: #include struct my_struct { int n; char s[] }; int main() { struct my_struct ms; ms.s =…
Tom
  • 6,601
  • 12
  • 40
  • 48
55
votes
5 answers

What is 'forward declaration' and the difference between 'typedef struct X' and 'struct X'?

I am a beginner in C programming and I know the difference between struct type declaration and typedef struct declaration. I came across to know an answer saying that if we define a struct like: typedef struct { some members; }…
r_goyal
  • 1,117
  • 1
  • 11
  • 20
55
votes
7 answers

Why does C have a distinction between -> and .?

OK, this is of no serious consequence, but it's been bugging me for a while: Is there a reason for the distinction between the -> and . operators? Of course, the current rule is that . acts on a struct, and -> acts on a pointer-to-struct (or union).…
bk.
  • 6,068
  • 2
  • 24
  • 28
54
votes
6 answers

Constructor for structs in C

Given: struct objStruct { int id; int value; }; typedef struct objStruct Object; Is there a shortcut to allocate and initialize the object, something like a C++ constructor? It could even be a preprocessor macro. Whatever makes the code…
Aillyn
  • 23,354
  • 24
  • 59
  • 84
54
votes
3 answers

indirect enums and structs

To start off, I want to say that I'm aware there are many articles and questions within SO that refer to the indirect keyword in Swift. The most popular explanation for the usage of indirect is to allow for recursive enums. Rather than just…
Kelvin Lau
  • 6,373
  • 6
  • 34
  • 57
54
votes
2 answers

Why is it necessary to call :this() on a struct to use automatic properties in c#?

If I define a struct in C# using automatic properties like this: public struct Address { public Address(string line1, string line2, string city, string state, string zip) { Line1 = line1; Line2 = line2; City = city; …
NerdFury
  • 18,876
  • 5
  • 38
  • 41
54
votes
2 answers

Invoking struct function gives "cannot refer to unexported field or method"

I have a structure something like this: type MyStruct struct { Id string } and function: func (m *MyStruct) id() { // doing something with id here } Also I have another structure like this: type MyStruct2 struct { m *MyStruct } Now…
0xAX
  • 20,957
  • 26
  • 117
  • 206
54
votes
5 answers

What is the meaning of `struct X typedef` vs. `typedef struct X`?

I have the following (working) code in an existing code base, used in include file that is shared between C and C++, compiling on MSVC (2010) and Windows DDK: struct X { USHORT x; } typedef X, *PX; And: enum MY_ENUM { enum_item_1, …
Itaypk
  • 1,103
  • 10
  • 25
53
votes
4 answers

Struct assignment or memcpy?

If I want to replicate a structure in another one (in C), what are the pro&con's of : struct1 = struct2; vs memcpy(&struct1, &struct2, sizeof(mystruct_t)); Are they equivalent ? Is there a difference in performance or memory use ?
ofaurax
  • 1,417
  • 1
  • 20
  • 27
53
votes
6 answers

Cannot use mutating member on immutable value of type

I have following struct: public protocol SuperModel { // empty protocol } struct ModelOne: SuperModel { struct SubModelOne { var someVar: Double var othervar: Double? } var sub: SubModelOne? mutating func…
j0h4nn3s
  • 2,016
  • 2
  • 20
  • 35
53
votes
7 answers

Anonymous union within struct not in c99?

here is very simplified code of problem I have: enum node_type { t_int, t_double }; struct int_node { int value; }; struct double_node { double value; }; struct node { enum node_type type; union { struct int_node…
Martin
  • 1,473
  • 2
  • 12
  • 20
53
votes
3 answers

Get struct field tag using Go reflect package

Is it possible to reflect on a field of a struct, and get a reference to its tag values? For example: type User struct { name string `json:name-field` age int } // ... user := &User{"John Doe The Fourth",…
Jameo
  • 4,507
  • 8
  • 40
  • 66
53
votes
4 answers

Is an empty initializer list valid C code?

It is common to use {0} to initialize a struct or an array but consider the case when the first field isn't a scalar type. If the first field of struct Person is another struct or array, then this line will result in an error (error: missing braces…
Marcus Ahlberg
  • 1,329
  • 2
  • 11
  • 24
53
votes
7 answers

Why doesn't GCC optimize structs?

Systems demand that certain primitives be aligned to certain points within the memory (ints to bytes that are multiples of 4, shorts to bytes that are multiples of 2, etc.). Of course, these can be optimized to waste the least space in padding. My…
Alex Gartrell
  • 2,514
  • 5
  • 22
  • 23