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
176
votes
3 answers

What is the meaning of "__attribute__((packed, aligned(4))) "

It is C language.It is written that: typedef struct __attribute__((packed, aligned(4))) Ball { float2 delta; float2 position; //float3 color; float size; //int arcID; //float arcStr; } Ball_t; Ball_t *balls; Please tell me…
Aaron Lee
  • 2,060
  • 2
  • 15
  • 20
175
votes
6 answers

Assign one struct to another in C

Can you assign one instance of a struct to another, like so: struct Test t1; struct Test t2; t2 = t1; I have seen it work for simple structures, bu does it work for complex structures? How does the compiler know how to copy data items depending on…
shreyasva
  • 13,126
  • 25
  • 78
  • 101
170
votes
14 answers

Convenient C++ struct initialisation

I'm trying to find a convenient way to initialise 'pod' C++ structs. Now, consider the following struct: struct FooBar { int foo; float bar; }; // just to make all examples work in C and C++: typedef struct FooBar FooBar; If I want to…
bitmask
  • 32,434
  • 14
  • 99
  • 159
167
votes
4 answers

Initializing a struct to 0

If I have a struct like this: typedef struct { unsigned char c1; unsigned char c2; } myStruct; What would be the easiest way to initialize this struct to 0? Would the following suffice? myStruct _m1 = {0}; or Would I need to explicitly…
Daan Timmer
  • 14,771
  • 6
  • 34
  • 66
163
votes
2 answers

Go Interface Fields

I'm familiar with the fact that, in Go, interfaces define functionality, rather than data. You put a set of methods into an interface, but you are unable to specify any fields that would be required on anything that implements that interface. For…
Matt Mc
  • 8,882
  • 6
  • 53
  • 89
157
votes
9 answers

self referential struct definition?

I haven't been writing C for very long, and so I'm not sure about how I should go about doing these sorts of recursive things... I would like each cell to contain another cell, but I get an error along the lines of "field 'child' has incomplete…
Ziggy
  • 21,845
  • 28
  • 75
  • 104
145
votes
10 answers

Why don't structs support inheritance?

I know that structs in .NET do not support inheritance, but its not exactly clear why they are limited in this way. What technical reason prevents structs from inheriting from other structs?
Juliet
  • 80,494
  • 45
  • 196
  • 228
142
votes
8 answers

How to search for an element in a golang slice

I have a slice of structs. type Config struct { Key string Value string } // I form a slice of the above struct var myconfig []Config // unmarshal a response body into the above slice if err := json.Unmarshal(respbody, &myconfig); err !=…
codec
  • 7,978
  • 26
  • 71
  • 127
138
votes
5 answers

Should struct definitions go in .h or .c file?

I've seen both full definitions of structs in headers and just declarations—is there any advantage to one method over the other? If it makes a difference, I usually typedef a struct like so in the .h typedef struct s s_t; To be clear, the options…
Aaron Yodaiken
  • 19,163
  • 32
  • 103
  • 184
137
votes
10 answers

Structs in Javascript

Previously, when I needed to store a number of related variables, I'd create a class. function Item(id, speaker, country) { this.id = id; this.speaker = speaker; this.country = country; } var myItems = [new Item(1, 'john', 'au'), new Item(2,…
nickf
  • 537,072
  • 198
  • 649
  • 721
135
votes
9 answers

sizeof single struct member in C

I am trying to declare a struct that is dependent upon another struct. I want to use sizeof to be safe/pedantic. typedef struct _parent { float calc ; char text[255] ; int used ; } parent_t ; Now I want to declare a struct child_t that has…
kevinarpe
  • 20,319
  • 26
  • 127
  • 154
135
votes
7 answers

C/C++ Struct vs Class

After finishing my C++ class it seemed to me the structs/classes are virtually identical except with a few minor differences. I've never programmed in C before; but I do know that it has structs. In C is it possible to inherit other structs and set…
anon235370
134
votes
7 answers

Why declare a struct that only contains an array in C?

I came across some code containing the following: struct ABC { unsigned long array[MAX]; } abc; When does it make sense to use a declaration like this?
Joe.Z
  • 2,725
  • 4
  • 25
  • 30
134
votes
16 answers

default value for struct member in C

Is it possible to set default values for some struct member? I tried the following but, it'd cause syntax error: typedef struct { int flag = 3; } MyStruct; Errors: $ gcc -o testIt test.c test.c:7: error: expected ‘:’, ‘,’, ‘;’, ‘}’ or…
user1508893
  • 9,223
  • 14
  • 45
  • 57
134
votes
10 answers

How do you make an array of structs in C?

I'm trying to make an array of structs where each struct represents a celestial body. I don't have that much experience with structs, which is why I decided to try to use them instead of a whole bunch of arrays. However, I keep on running into…
Amndeep7
  • 2,022
  • 4
  • 17
  • 26