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

Why do C and C++ support memberwise assignment of arrays within structs, but not generally?

I understand that memberwise assignment of arrays is not supported, such that the following will not work: int num1[3] = {1,2,3}; int num2[3]; num2 = num1; // "error: invalid array assignment" I just accepted this as fact, figuring that the aim of…
ozmo
  • 992
  • 1
  • 7
  • 12
92
votes
5 answers

Why is this struct size 3 instead of 2?

I have defined this struct: typedef struct { char A:3; char B:3; char C:3; char D:3; char E:3; } col; The sizeof(col) give me the output of 3, but shouldn't it be 2? If I comment just one element, the sizeof is 2. I don't…
Raffaello
  • 1,641
  • 15
  • 29
91
votes
9 answers

Changing The value of struct in an array

I want to store structs inside an array, access and change the values of the struct in a for loop. struct testing { var value:Int } var test1 = testing(value: 6 ) test1.value = 2 // this works with no issue var test2 = testing(value: 12…
reza23
  • 3,079
  • 2
  • 27
  • 42
91
votes
5 answers

Why is the new Tuple type in .Net 4.0 a reference type (class) and not a value type (struct)

Does anyone know the answer and/or have an opinion about this? Since tuples would normally not be very large, I would assume it would make more sense to use structs than classes for these. What say you?
Bent Rasmussen
  • 5,538
  • 9
  • 44
  • 63
90
votes
12 answers

What's the best way to put a c-struct in an NSArray?

What's the usual way to store c-structures in an NSArray? Advantages, disadvantages, memory handling? Notably, what's the difference between valueWithBytes and valueWithPointer -- raised by justin and catfish below. Here's a link to Apple's…
Fattie
  • 27,874
  • 70
  • 431
  • 719
88
votes
7 answers

Why are interfaces needed in Golang?

In Golang, we use structs with receiver methods. everything is perfect up to here. I'm not sure what interfaces are, however. We define methods in structs and if we want to implement a method on a struct, we write it anyway again under another…
nikoss
  • 3,254
  • 2
  • 26
  • 40
87
votes
12 answers

Is it safe to return a struct in C or C++?

What I understand is that this shouldn't be done, but I believe I've seen examples that do something like this (note code is not necessarily syntactically correct but the idea is there) typedef struct{ int a,b; }mystruct; And then here's a…
jzepeda
  • 1,470
  • 2
  • 15
  • 22
87
votes
16 answers

When to use bit-fields in C

On the question 'why do we need to use bit-fields?', searching on Google I found that bit fields are used for flags. Now I am curious, Is it the only way bit-fields are used practically? Do we need to use bit fields to save space? A way of…
YohanRoth
  • 3,153
  • 4
  • 31
  • 58
87
votes
11 answers

In Go, how can I convert a struct to a byte array?

I have an instance of a struct that I defined and I would like to convert it to an array of bytes. I tried []byte(my_struct), but that did not work. Also, I was pointed to the binary package, but I am not sure which function I should use and how I…
abw333
  • 5,571
  • 12
  • 41
  • 48
86
votes
7 answers

Copy struct to struct in C

I want to copy an identical struct into another and later on use it as a comparance to the first one. The thing is that my compiler gives me a warning when Im doing like this! Should I do it in another way or am I doing this wrong: In header…
Christian
  • 1,548
  • 2
  • 15
  • 26
86
votes
5 answers

Does the order of members in a struct matter?

I have found a peculiar behaviour in C. Consider the below code: struct s { int a; }; struct z { int a; struct s b[]; }; int main(void) { return 0; } It compiles just fine. Then change the order of the members…
Milo Wielondek
  • 4,164
  • 3
  • 33
  • 45
86
votes
6 answers

Define a struct inside a class in C++

Can someone give me an example about how to define a new type of struct in a class in C++. Thanks.
small_potato
  • 3,127
  • 5
  • 39
  • 45
85
votes
4 answers

ARC forbids Objective-C objects in structs or unions despite marking the file -fno-objc-arc

ARC forbids Objective-C objects in structs or unions despite marking the file -fno-objc-arc? Why is this so? I had the assumption that if you mark it -fno-objc-arc you don't have this restriction.
Zsolt
  • 3,648
  • 3
  • 32
  • 47
85
votes
2 answers

Volatile Struct Semantics

Is it sufficient to declare an instance of a structure-typed variable as volatile (if its fields are accessed in re-entrant code), or must one declare specific fields of the structure as volatile? Phrased differently, what are the semantic…
vicatcu
  • 5,407
  • 7
  • 41
  • 65
84
votes
9 answers

Save Struct to UserDefaults

I have a struct that I want to save to UserDefaults. Here's my struct struct Song { var title: String var artist: String } var songs: [Song] = [ Song(title: "Title 1", artist "Artist 1"), Song(title: "Title 2", artist "Artist 2"), …
Jacob Cavin
  • 2,169
  • 3
  • 19
  • 47