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
1619
votes
31 answers

When should I use a struct rather than a class in C#?

When should you use struct and not class in C#? My conceptual model is that structs are used in times when the item is merely a collection of value types. A way to logically hold them all together into a cohesive whole. I came across these rules…
Alex Baranosky
  • 48,865
  • 44
  • 102
  • 150
1257
votes
27 answers

When should you use a class vs a struct in C++?

In what scenarios is it better to use a struct vs a class in C++?
Alan Hinchcliffe
  • 12,862
  • 3
  • 18
  • 11
1015
votes
12 answers

typedef struct vs struct definitions

I'm a beginner in C programming, but I was wondering what's the difference between using typedef when defining a structure versus not using typedef. It seems to me like there's really no difference, they accomplish the same goal. struct myStruct{ …
user69514
  • 26,935
  • 59
  • 154
  • 188
955
votes
8 answers

Difference between 'struct' and 'typedef struct' in C++?

In C++, is there any difference between: struct Foo { ... }; and: typedef struct { ... } Foo;
criddell
  • 14,289
  • 9
  • 41
  • 45
892
votes
19 answers

What's the difference between struct and class in .NET?

What's the difference between struct and class in .NET?
Keith
  • 150,284
  • 78
  • 298
  • 434
851
votes
13 answers

Why isn't sizeof for a struct equal to the sum of sizeof of each member?

Why does the sizeof operator return a size larger for a structure than the total sizes of the structure's members?
Kevin
  • 25,207
  • 17
  • 54
  • 57
697
votes
31 answers

How to print struct variables in console?

How can I print (to the console) the Id, Title, Name, etc. of this struct in Golang? type Project struct { Id int64 `json:"project_id"` Title string `json:"title"` Name string `json:"name"` Data Data …
fnr
  • 9,007
  • 5
  • 17
  • 16
661
votes
27 answers

C-like structures in Python

Is there a way to conveniently define a C-like structure in Python? I'm tired of writing stuff like: class MyStruct(): def __init__(self, field1, field2, field3): self.field1 = field1 self.field2 = field2 self.field3 =…
wesc
  • 6,631
  • 3
  • 18
  • 5
606
votes
16 answers

How to initialize a struct in accordance with C programming language standards

I want to initialize a struct element, split in declaration and initialization. This is what I have: typedef struct MY_TYPE { bool flag; short int value; double stuff; } MY_TYPE; void function(void) { MY_TYPE a; ... a = { true, 15,…
cringe
  • 13,401
  • 15
  • 69
  • 102
588
votes
4 answers

What are the use(s) for struct tags in Go?

In the Go Language Specification, it mentions a brief overview of tags: A field declaration may be followed by an optional string literal tag, which becomes an attribute for all the fields in the corresponding field declaration. The tags are…
liamzebedee
  • 14,010
  • 21
  • 72
  • 118
560
votes
17 answers

Why Choose Struct Over Class?

Playing around with Swift, coming from a Java background, why would you want to choose a Struct instead of a Class? Seems like they are the same thing, with a Struct offering less functionality. Why choose it then?
bluedevil2k
  • 9,366
  • 8
  • 43
  • 57
553
votes
16 answers

Why are mutable structs “evil”?

Following the discussions here on SO I already read several times the remark that mutable structs are “evil” (like in the answer to this question). What's the actual problem with mutability and structs in C#?
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
542
votes
15 answers

Why should we typedef a struct so often in C?

I have seen many programs consisting of structures like the one below typedef struct { int i; char k; } elem; elem user; Why is it needed so often? Any specific reason or applicable area?
Manoj Doubts
  • 13,579
  • 15
  • 42
  • 45
496
votes
17 answers

Struct Constructor in C++?

Can a struct have a constructor in C++? I have been trying to solve this problem but I am not getting the syntax.
Jay
492
votes
29 answers

What are the differences between struct and class in C++?

This question was already asked in the context of C#/.Net. Now I'd like to learn the differences between a struct and a class in C++. Please discuss the technical differences as well as reasons for choosing one or the other in OO design. I'll start…
palm3D
  • 7,970
  • 6
  • 28
  • 33
1
2 3
99 100