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
7
votes
4 answers

How to assign char array in struct inline?

I'm trying to do something like this: struct SomeStruct { const char *bytes; const char *desc; }; SomeStruct example = { { 0x10, 0x11, 0x12, 0x13 }, "10-13" }; Why isn't this working?
d0c_s4vage
  • 3,947
  • 6
  • 23
  • 32
7
votes
1 answer

Producer/Consumer with C# structs?

I have a singleton object that process requests. Each request takes around one millisecond to be completed, usually less. This object is not thread-safe and it expects requests in a particular format, encapsulated in the Request class, and returns…
Vlad
  • 802
  • 1
  • 10
  • 23
7
votes
3 answers

C array of struct

typedef struct Expected { const int number; const char operand; } Expected; Expected array[1]; Expected e = {1, 'c'}; array[0] = e; I don't understand why you cannot add to a struct array like that. Do I have to calculate the positions…
Aristos Georgiou
  • 315
  • 2
  • 13
7
votes
4 answers

Spark : Union can only be performed on tables with the compatible column types. Struct != Struct

Error : Union can only be performed on tables with the compatible column types. struct(tier:string,skyward_number:string,skyward_points:string) <> struct(skyward_number:string,tier:string,skyward_points:string) at the first column of the…
Ravi
  • 198
  • 1
  • 1
  • 8
7
votes
1 answer

What is a function for structs like Java's instanceof?

I'm making an OOP chat client in Rust. The module messages.rs creates and handles messages to other modules as structs: SimpleMessage and ComplexMessage structs: //! # Messages use time::SteadyTime; /// Represents a simple text message pub struct…
Dialvive
  • 356
  • 7
  • 19
7
votes
4 answers

How to update value of two struct efficiently

I have the following code which parses YAML files and needs to match values from one struct external and update the internal struct's type property. For example, this is the yaml file(translated to bin for simplicity) and content which is parsed…
user4445419
7
votes
2 answers

Is it legal to access struct members via offset pointers from other struct members?

In these two examples, does accessing members of the struct by offsetting pointers from other members result in Undefined / Unspecified / Implementation Defined Behavior? struct { int a; int b; } foo1 = {0, 0}; (&foo1.a)[1] = 1; printf("%d",…
AJMansfield
  • 4,039
  • 3
  • 29
  • 50
7
votes
6 answers

How can I unpack a length-prefixed bytestring?

I used the standard library struct module to pack a bytes object into a string, preceded by length information: >>> import struct >>> string = b'-' >>> t = struct.pack(">h%ds" % len(string), len(string), string) >>> print(t) b'\x00\x01-' Of course…
dbdii407
  • 815
  • 3
  • 11
  • 14
7
votes
2 answers

Why does setting the property in the constructor of a struct not work?

I have following code which is not allowed (error below), why? struct A { private int b; public A(int x) { B = x; } public int B { get { return b; } set {…
Mocco
  • 1,183
  • 2
  • 12
  • 25
7
votes
3 answers

Does standard C accept `{0}` as an initializer for any struct?

This is often suggested, as a way to initialize a struct to zero values: struct foo f = {0}; It is also mentioned that {} could be used under gcc, but that this is not standard C99. I wonder if this works for a struct whose layout may vary outside…
sourcejedi
  • 3,051
  • 2
  • 24
  • 42
7
votes
3 answers

sizeof(), alignment in C structs:

Preface: Did my research about struct alignment. Looked at this question, this one and also this one - but still did not find my answer. My Actual Question: Here is a code snippet I created in order to clarify my question: #include…
MordechayS
  • 1,552
  • 2
  • 19
  • 29
7
votes
2 answers

How to access structure in other program's memory?

I know how to import and use read/writeprocessmomory in C#. I'm working on game trainer. I need to have "direct" access to other process memory casted to struct. I can use readprocessmemory or writeprocessmemory but that would take much time to…
Hooch
  • 28,817
  • 29
  • 102
  • 161
7
votes
1 answer

Encapsulating struct in Go

I am new to Go. I have read that encapsulation in Go is on the package level. I have a simple web controller use case. I have a struct which comes in as a JSON object and is Unmarshaled into the struct type. type User struct{ Name String…
rhumbaJi
  • 73
  • 1
  • 4
7
votes
2 answers

Expected to decode Dictionary but found a string/data instead

I'm working with the docodable protocol and I'm having this error: typeMismatch(Swift.Dictionary, Swift.DecodingError.Context(codingPath: [MakeApp_v2.Params.(CodingKeys in _244BBB2F32A8C5CF3DB84B0C6A94B232).config,…
riogarrell
  • 103
  • 1
  • 10
7
votes
3 answers

How to use reflect to recursively parse nested struct in Go?

I have a nested three layer struct. I would like to use reflect in Go to parse it (use recursive function). The reasons to use reflect and the recursive function are can have various number of fields (but the first two fields are fixed) the field…
Luffy Cyliu
  • 811
  • 1
  • 9
  • 11