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

26750 questions
1485
votes
30 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
  • 44,548
  • 39
  • 95
  • 146
1084
votes
26 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
  • 11,094
  • 3
  • 16
  • 11
894
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
  • 24,321
  • 56
  • 146
  • 183
879
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
  • 13,189
  • 9
  • 39
  • 44
768
votes
19 answers

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

What's the difference between struct and class in .NET?
Keith
  • 133,927
  • 68
  • 273
  • 391
752
votes
12 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
  • 22,827
  • 15
  • 51
  • 56
519
votes
25 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
  • 5,211
  • 3
  • 15
  • 5
514
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
  • 11,927
  • 14
  • 65
  • 100
513
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
  • 161,833
  • 52
  • 243
  • 303
509
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
  • 8,568
  • 6
  • 39
  • 54
481
votes
24 answers

How to print struct variables in console?

How can I print (in 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
  • 6,297
  • 5
  • 11
  • 15
461
votes
30 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,419
  • 5
  • 25
  • 32
457
votes
3 answers

What are the use(s) for 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
  • 12,096
  • 19
  • 65
  • 114
447
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
  • 11,957
  • 15
  • 39
  • 42
436
votes
15 answers

Difference between a Structure and a Union

Is there any good example to give the difference between a struct and a union? Basically I know that struct uses all the memory of its member and union uses the largest members memory space. Is there any other OS level difference?
gagneet
  • 31,111
  • 28
  • 70
  • 99
1
2 3
99 100