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

Forward declarations of unnamed struct

Bounty question: So, these two Foos aren't the same thing. Fine. The second form is given in a library. How do I forward-declare it given that I can't change it? I always thought C and C++ allowed repeated declarations provided that there were no…
spraff
  • 32,570
  • 22
  • 121
  • 229
46
votes
5 answers

The initialization of static variables in C

I have a question about the initialization of static variables in C. I know if we declare a global static variable that by default the value is 0. For example: static int a; //although we do not initialize it, the value of a is 0 but what about the…
user707549
46
votes
10 answers

How can I check whether a struct has been instantiated?

I have a struct that (for the purposes of this question) pretty much mimics the built in Point type. I need to check that it has been instantiated before using it. When it was Point, I could do this: if (this.p == null) But that now generates the…
Tom Wright
  • 11,278
  • 15
  • 74
  • 148
45
votes
9 answers

Are structs 'pass-by-value'?

I've recently tried to create a property for a Vector2 field, just to realize that it doesn't work as intended. public Vector2 Position { get; set; } this prevents me from changing the values of its members (X & Y) Looking up information on this, I…
Acidic
  • 6,154
  • 12
  • 46
  • 80
45
votes
3 answers

Missing type in composite literal

type A struct { B struct { Some string Len int } } Simple question. How to initialize this struct? I would like to do something like this: a := &A{B:{Some: "xxx", Len: 3}} Expectedly I'm getting an error: missing type in…
tacobot
  • 915
  • 2
  • 9
  • 15
45
votes
4 answers

Initialize embedded struct in Go

I have the following struct which contains a net/http.Request: type MyRequest struct { http.Request PathParams map[string]string } Now I want to initialize the anonymous inner struct http.Request in the following function: func…
deamon
  • 89,107
  • 111
  • 320
  • 448
44
votes
9 answers

Fastest way to pack a list of floats into bytes in python

I have a list of say 100k floats and I want to convert it into a bytes buffer. buf = bytes() for val in floatList: buf += struct.pack('f', val) return buf This is quite slow. How can I make it faster using only standard Python 3.x libraries.
MxLDevs
  • 19,048
  • 36
  • 123
  • 194
44
votes
3 answers

How does native implementation of ValueType.GetHashCode work?

I created two structures of TheKey type k1={17,1375984} and k2={17,1593144}. Obviosly the pointers in the second fields are different. But both get same hash code=346948941. Expected to see different hash codes. See the code below. struct TheKey { …
user63289
44
votes
4 answers

Hide parameterless constructor on struct

Is it possible to hide the parameterless constructor from a user in C#? I want to force them to always use the constructor with parameters e.g. this Position struct public struct Position { private readonly int _xposn; private readonly int…
ChrisCa
  • 10,876
  • 22
  • 81
  • 118
44
votes
3 answers

golang - how to initialize a map field within a struct?

I'm confused about the best way to initialize a struct that contains a map. Running this code produces panic: runtime error: assignment to entry in nil map: package main type Vertex struct { label string } type Graph struct { connections…
Matt
  • 4,815
  • 5
  • 39
  • 40
44
votes
4 answers

Why are .NET value types sealed?

It's not possible to inherit from a C# struct. It's not obvious to me why this is: Clearly you can't have a reference type that inherits from a value type; this wouldn't work It doesn't sound reasonable to inherit from one the primitive types…
Tim Robinson
  • 53,480
  • 10
  • 121
  • 138
44
votes
5 answers

Correct way of initializing a struct in a class constructor

So I want to add a struct from a c header file as a class member to a c++ class. But I get a compiler error for the cpp file: bar was not declared inn this scope. This is what I have: // myClass.hpp #include fileWithStruct.h class myClass { …
tzippy
  • 6,458
  • 30
  • 82
  • 151
43
votes
7 answers

How is the result struct of localtime allocated in C?

I was playing with the time.h file in C that helps us with time/day functions. I came across: struct tm * _Cdecl localtime(const time_t *__timer); ...which seems to return a pointer to tm struct. I have found that return by address is mostly used…
Akash
  • 4,956
  • 11
  • 42
  • 70
43
votes
4 answers

In C++, is it valid to treat scalar members of a struct as if they comprised an array?

While looking at the code for Dear Imgui, I found the following code (edited for relevance): struct ImVec2 { float x, y; float& operator[] (size_t idx) { return (&x)[idx]; } }; It's pretty clear that this works in practice, but from the…
43
votes
5 answers

Why is this implemented as a struct?

In System.Data.Linq, EntitySet uses a couple of ItemList structs which look like this: internal struct ItemList where T : class { private T[] items; private int count; ...(methods)... } (Took me longer than it should to…
Simon Hewitt
  • 1,391
  • 9
  • 24