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
68
votes
8 answers

Generic constraints, where T : struct and where T : class

I would like to differentiate between following cases: A plain value type (e.g. int) A nullable value type (e.g. int?) A reference type (e.g. string) - optionally, I would not care if this mapped to (1) or (2) above I have come up with the…
Pierre Arnaud
  • 10,212
  • 11
  • 77
  • 108
68
votes
3 answers

How to set and get fields in struct's method

After creating a struct like this: type Foo struct { name string } func (f Foo) SetName(name string) { f.name = name } func (f Foo) GetName() string { return f.name } How do I create a new instance of Foo and set and get the name? I…
nope
  • 1,060
  • 2
  • 15
  • 32
67
votes
3 answers

How to assign a C struct inline?

typedef struct { int hour; int min; int sec; } counter_t; And in the code, I'd like to initialize instances of this struct without explicitly initializing each member variable. That is, I'd like to do something like: counter_t…
mindthief
  • 12,755
  • 14
  • 57
  • 61
66
votes
2 answers

What does C++ syntax “A::B:A {};” mean

What does C++ syntax struct A::B:A {}; mean? Where is this name definition (or access) described in the C++ standard? #include struct B; struct A { struct B; }; struct A::B:A { }; int main() { A::B::A::B b; …
devsh
  • 549
  • 1
  • 4
  • 7
66
votes
1 answer

When to use pointers

I'm new to the Go Language, and have only minimal background in C/C++, so naturally I'm struggling with the idea of when to use pointers and when not to use pointers. Although this question might be considered open-ended, I'm wondering what some…
Jim Pedid
  • 2,730
  • 3
  • 23
  • 27
66
votes
8 answers

Julia: OOP or not

I'm working on Juno with Julia. I don't know if Julia supports OOP or not. For example, is there something like class or struct of c++? How to declare it with members such as a data or a function?
Yves
  • 11,597
  • 17
  • 83
  • 180
66
votes
7 answers

Why is there no RAII in .NET?

Being primarily a C++ developer the absence of RAII (Resource Acquisition Is Initialization) in Java and .NET has always bothered me. The fact that the onus of cleaning up is moved from the class writer to its consumer (by means of try finally or…
Motti
  • 110,860
  • 49
  • 189
  • 262
65
votes
6 answers

How can I correctly assign a new string value?

I'm trying to understand how to solve this trivial problem in C, in the cleanest/safest way. Here's my example: #include int main(int argc, char *argv[]) { typedef struct { char name[20]; char surname[20]; …
Gianluca Bargelli
  • 1,780
  • 2
  • 17
  • 23
65
votes
5 answers

How to set bool pointer to true in struct literal?

I have the function below which accepts a bool pointer. I'm wondering if there is any notation which allows me to set the value of the is field to true in the struct literal; basically without to define a new identifier (i.e. var x := true ;…
The user with no hat
  • 10,166
  • 20
  • 57
  • 80
65
votes
6 answers

Overriding Equals method in Structs

I've looked for overriding guidelines for structs, but all I can find is for classes. At first I thought I wouldn't have to check to see if the passed object was null, as structs are value types and can't be null. But now that I come to think of it,…
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
64
votes
6 answers

When are anonymous structs and unions useful in C11?

C11 adds, among other things, 'Anonymous Structs and Unions'. I poked around but could not find a clear explanation of when anonymous structs and unions would be useful. I ask because I don't completely understand what they are. I get that they are…
griotspeak
  • 13,022
  • 13
  • 43
  • 54
64
votes
3 answers

Automatic Properties and Structures Don't Mix?

Kicking around some small structures while answering this post, I came across the following unexpectedly: The following structure, using an int field is perfectly legal: struct MyStruct { public MyStruct ( int size ) { this.Size =…
Mike Rosenblum
  • 12,027
  • 6
  • 48
  • 64
64
votes
4 answers

size of struct in C

Possible Duplicate: Why isn’t sizeof for a struct equal to the sum of sizeof of each member? Consider the following C code: #include struct employee { int id; char name[30]; }; int main() { struct employee e1; …
user191776
63
votes
6 answers

In C, how would I choose whether to return a struct or a pointer to a struct?

Working on my C muscle lately and looking through the many libraries I've been working with its certainly gave me a good idea of what is good practice. One thing that I have NOT seen is a function that returns a struct: something_t make_something()…
Dellowar
  • 3,160
  • 1
  • 18
  • 37
63
votes
3 answers

C vs C++ struct alignment

I've been asked in a recent interview about C++ struct fields alignment and theoretized that C and C++ follows the same strategy in struct packing. Hovewer, it was the wrong assumption. The interviewer said that in general C and C++ are packing…
Minor Threat
  • 2,025
  • 1
  • 18
  • 32