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
74
votes
12 answers

Using Python class as a data container

Sometimes it makes sense to cluster related data together. I tend to do so with a dict, e.g., group = dict(a=1, b=2, c=3) print(group['a']) One of my colleagues prefers to create a class class groupClass: def __init__(self, a, b, c): …
new name
  • 15,861
  • 19
  • 68
  • 114
74
votes
2 answers

Return value optimization and copy elision in C

Some people are not aware that it's possible to pass and return structs by value in C. My question is about the compiler making unnecessary copies when returning structs in C. Do C compilers such as GCC use Return value optimization(RVO)…
Z boson
  • 32,619
  • 11
  • 123
  • 226
74
votes
8 answers

How to check programmatically if a type is a struct or a class?

How to check programmatically if a type is a struct or a class?
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
72
votes
3 answers

How to clone a struct storing a boxed trait object?

I wrote a program that has the trait Animal and the struct Dog implementing the trait. It also has a struct AnimalHouse storing an animal as a trait object Box. trait Animal { fn speak(&self); } struct Dog { name: String, } impl…
Denis Kreshikhin
  • 8,856
  • 9
  • 52
  • 84
71
votes
7 answers

dereferencing pointer to incomplete type

I've seen a lot of questions on this but I'm going to ask the question differently without specific code. Is there a way of EASILY determining what is causing the type to be incomplete? In my case I'm using someone elses code and I'm completely…
nick
  • 1,055
  • 1
  • 10
  • 17
71
votes
6 answers

C : typedef struct name {...}; VS typedef struct{...} name;

As the title says, I have this code: typedef struct Book{ int id; char title[256]; char summary[2048]; int numberOfAuthors; struct Author *authors; }; typedef struct Author{ char…
Alek Sobczyk
  • 813
  • 1
  • 7
  • 6
70
votes
8 answers

Non-unique enum values

I am trying to obscure the index positions on an edi file... I had a situation where 2 or 3 things could be at an index based on the situation. It'd be cool to use an enum to hide the "magic numbers" and was suprised to see that you could assign…
Rikon
  • 2,688
  • 3
  • 22
  • 32
70
votes
3 answers

How to make a 'struct' Nullable by definition?

struct AccountInfo { String Username; String Password; } now if I want to have a Nullable instance I should write: Nullable myAccount = null; But I want make the struct Nullable by nature and it can be used like this (without…
Xaqron
  • 29,931
  • 42
  • 140
  • 205
70
votes
3 answers

Initialize nested struct definition

How do you initialize the following struct? type Sender struct { BankCode string Name string Contact struct { Name string Phone string } } I tried: s := &Sender{ BankCode: "BC", Name: …
Kiril
  • 6,009
  • 13
  • 57
  • 77
70
votes
10 answers

How to compile C code with anonymous structs / unions?

I can do this in c++/g++: struct vec3 { union { struct { float x, y, z; }; float xyz[3]; }; }; Then, vec3 v; assert(&v.xyz[0] == &v.x); assert(&v.xyz[1] == &v.y); assert(&v.xyz[2] == &v.z); will…
solinent
  • 1,605
  • 1
  • 17
  • 19
69
votes
1 answer

What is the difference between static_cast and reinterpret_cast?

Possible Duplicate: When should static_cast, dynamic_cast and reinterpret_cast be used? I'm using c function in c++, where a structure passed as a void type argument in c is directly stored that same structure type. eg in C. void getdata(void…
HariHaraSudhan
  • 1,330
  • 3
  • 15
  • 24
69
votes
4 answers

Struct padding in C++

If I have a struct in C++, is there no way to safely read/write it to a file that is cross-platform/compiler compatible? Because if I understand correctly, every compiler 'pads' differently based on the target platform.
Baruch
  • 20,590
  • 28
  • 126
  • 201
69
votes
5 answers

"error: assignment to expression with array type error" when I assign a struct field (C)

I'm a beginner C programmer, yesterday I learned the use of C structs and the possible application of these ones about the resolution of specific problems. However when I was experimenting with my C IDE (Codeblocks 16.01) in order to learn this…
p4nzer96
  • 815
  • 1
  • 7
  • 6
68
votes
2 answers

Why are HashSets of structs with nullable values incredibly slow?

I investigated performance degradation and tracked it down to slow HashSets. I have structs with nullable values that are used as a primary key. For example: public struct NullableLongWrapper { private readonly long? _value; public…
Kobi
  • 135,331
  • 41
  • 252
  • 292
68
votes
3 answers

Import struct from another package and file golang

I have a problem trying to import a type from another package and file. The struct that I'm trying to import is the one underneath. type PriorityQueue []*Item type Item struct { value string priority int index int } If I would put the…
Jakob Svenningsson
  • 4,175
  • 6
  • 23
  • 26