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

What does C++ struct syntax "a : b" mean

If I have a C++ struct, defining a 64bit data word such as.. struct SMyDataWord { int Name : 40; int Colour : 24; }; What does the : 40 syntax mean... does it mean that the first 40 bits are reserved for the Name and the remaining 24 bits…
Krakkos
  • 1,439
  • 3
  • 19
  • 24
49
votes
1 answer

how to modify struct fields in golang

I have example play.golang.org/p/Y1KX-t5Sj9 where I define method Modify() on struct User type User struct { Name string Age int } func (u *User) Modify() { *u = User{Name: "Paul"} } in the main() I am defining struct literal &User{Name:…
irom
  • 3,316
  • 14
  • 54
  • 86
49
votes
2 answers

How to define a typedef struct containing pointers to itself?

I am writing a LinkedList in C, the below code represent my Node definition. typedef struct { int value; struct Node* next; struct Node* prev; } Node; I understand (or think that I do) that struct Node not the same as typedef struct…
Kenny Cason
  • 12,109
  • 11
  • 47
  • 72
49
votes
7 answers

Swift - How to mutate a struct object when iterating over it

I am still not sure about the rules of struct copy or reference. I want to mutate a struct object while iterating on it from an array: For instance in this case I would like to change the background color but the compiler is yelling at me struct…
Avba
  • 14,822
  • 20
  • 92
  • 192
49
votes
5 answers

Can C++ struct have member functions?

I was pretty confused about the difference between struct and class as I seemed to see them used for pretty much the same things. I googled the differences and the only answer I saw was that structs have public members by default and classes have…
sion
  • 1,367
  • 6
  • 17
  • 21
49
votes
4 answers

How to pack and unpack using ctypes (Structure <-> str)

This might be a silly question but I couldn't find a good answer in the docs or anywhere. If I use struct to define a binary structure, the struct has 2 symmetrical methods for serialization and deserialization (pack and unpack) but it seems ctypes…
Mr Temp
  • 493
  • 1
  • 4
  • 4
48
votes
9 answers

Are packed structs portable?

I have some code on a Cortex-M4 microcontroller and'd like to communicate with a PC using a binary protocol. Currently, I'm using packed structs using the GCC-specific packed attribute. Here is a rough outline: struct Sensor1Telemetry { int16_t…
Venemo
  • 18,515
  • 13
  • 84
  • 125
48
votes
7 answers

packing and unpacking variable length array/string using the struct module in python

I am trying to get a grip around the packing and unpacking of binary data in Python 3. Its actually not that hard to understand, except one problem: what if I have a variable length textstring and want to pack and unpack this in the most elegant…
agnsaft
  • 1,791
  • 7
  • 30
  • 49
48
votes
12 answers

Creating "classes" in C, on the stack vs the heap?

Whenever I see a C "class" (any struct that is meant to be used by accessing functions that take a pointer to it as the first argument) I see them implemented like this: typedef struct { int member_a; float member_b; } CClass; CClass*…
Therhang
  • 825
  • 1
  • 9
  • 15
48
votes
4 answers

How to find out the max value for Int in Swift

I want to understand how to access the "struct" type of Int. When I cmd-clicked Int it took me to this class, i want to find out what is the maximum value this can hold. Is there a way to pull from one of this properties ?. what is max and min in…
thndrkiss
  • 4,515
  • 8
  • 57
  • 96
48
votes
5 answers

C struct initialization using labels. It works, but how?

I found some struct initialization code yesterday that threw me for a loop. Here's an example: typedef struct { int first; int second; } TEST_STRUCT; void testFunc() { TEST_STRUCT test = { second: 2, first: 1 }; …
Andrew Cottrell
  • 3,312
  • 3
  • 26
  • 41
48
votes
7 answers

FirstOrDefault() result of a struct collection?

So I've got a collection of structs (it's actually a WCF datacontract but I'm presuming this has no bearing here). List OptionalExtras; OptionalExtra is a struct. public partial struct OptionalExtra Now I'm running the below…
Liam
  • 27,717
  • 28
  • 128
  • 190
47
votes
7 answers

Non-read only alternative to anonymous types

In C#, an anonymous type can be as follows: method doStuff(){ var myVar = new { a = false, b = true } if (myVar.a) { // Do stuff } } However, the following will not compile: method…
Superbest
  • 25,318
  • 14
  • 62
  • 134
47
votes
4 answers

Swift: Convert struct to JSON?

I created a struct and want to save it as a JSON-file. struct Sentence { var sentence = "" var lang = "" } var s = Sentence() s.sentence = "Hello world" s.lang = "en" print(s) ...which results in: Sentence(sentence: "Hello world", lang:…
ixany
  • 5,433
  • 9
  • 41
  • 65
46
votes
3 answers

How to initialize an array of struct in C++?

I have the following struct in my C++ code (I am using Visual Studio 2010): struct mydata { string scientist; double value; }; What I would like to do is to be able to initialize them in a quick way, similar to array initialization in C99…
Alexander Galkin
  • 12,086
  • 12
  • 63
  • 115