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
107
votes
7 answers

Proper way to initialize C++ structs

Our code involves a POD (Plain Old Datastructure) struct (it is a basic c++ struct that has other structs and POD variables in it that needs to get initialized in the beginning.) Based one what I've read, it seems that: myStruct =…
KC3BZU
  • 1,224
  • 2
  • 11
  • 14
107
votes
10 answers

Is it legal to index into a struct?

Regardless of how 'bad' the code is, and assuming that alignment etc are not an issue on the compiler/platform, is this undefined or broken behavior? If I have a struct like this :- struct data { int a, b, c; }; struct data thing; Is it legal…
jcoder
  • 29,554
  • 19
  • 87
  • 130
105
votes
11 answers

Why can't C compilers rearrange struct members to eliminate alignment padding?

Possible Duplicate: Why doesn't GCC optimize structs? Why doesn't C++ make the structure tighter? Consider the following example on a 32 bit x86 machine: Due to alignment constraints, the following struct struct s1 { char a; int b; …
bisgardo
  • 4,130
  • 4
  • 29
  • 38
105
votes
8 answers

Golang : Is conversion between different struct types possible?

Let's say I have two similar types set this way : type type1 []struct { Field1 string Field2 int } type type2 []struct { Field1 string Field2 int } Is there a direct way to write values from a type1 to a type2, knowing that they…
Nicolas Marshall
  • 4,186
  • 9
  • 36
  • 54
103
votes
8 answers

How to use SCNetworkReachability in Swift

I'm trying to convert this code snippet to Swift. I'm struggling on getting off the ground due to some difficulties. - (BOOL) connectedToNetwork { // Create zero addy struct sockaddr_in zeroAddress; bzero(&zeroAddress,…
Isuru
  • 30,617
  • 60
  • 187
  • 303
102
votes
3 answers

Struct memory layout in C

I have a C# background. I am very much a newbie to a low-level language like C. In C#, struct's memory is laid out by the compiler by default. The compiler can re-order data fields or pad additional bits between fields implicitly. So, I had to…
eonil
  • 83,476
  • 81
  • 317
  • 516
101
votes
11 answers

How to convert string to IP address and vice versa

how can I convert a string ipAddress (struct in_addr) and vice versa? and how do I turn in unsigned long ipAddress? thanks
Safari
  • 11,437
  • 24
  • 91
  • 191
101
votes
5 answers

Swift constants: Struct or Enum

I'm not sure which of both are better to define constants. A struct or a enum. A struct will be copied every time i use it or not? When i think about a struct with static let constants it makes no sense that it will copied all the time, in my…
Paixsn
  • 1,256
  • 3
  • 12
  • 22
100
votes
1 answer

What's the purpose of this [1] at the end of struct declaration?

I was snooping through my MSP430 microcontroller's header files, and I ran into this in : /* r3 does not have to be saved */ typedef struct { uint32_t __j_pc; /* return address */ uint32_t __j_sp; /* r1 stack pointer */ …
Alexander
  • 59,041
  • 12
  • 98
  • 151
99
votes
10 answers

Structs versus classes

I'm about to create 100,000 objects in code. They are small ones, only with 2 or 3 properties. I'll put them in a generic list and when they are, I'll loop them and check value a and maybe update value b. Is it faster/better to create these objects…
Michel
  • 23,085
  • 46
  • 152
  • 242
98
votes
8 answers

memset() or value initialization to zero out a struct?

In Win32 API programming it's typical to use C structs with multiple fields. Usually only a couple of them have meaningful values and all others have to be zeroed out. This can be achieved in either of the two ways: STRUCT theStruct; memset(…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
97
votes
5 answers

Vector of structs initialization

I want know how I can add values to my vector of structs using the push_back method struct subject { string name; int marks; int credits; }; vector sub; So now how can I add elements to it? I have function that initializes string…
SRN
  • 2,418
  • 3
  • 22
  • 26
96
votes
6 answers

Does an unused data member take up memory?

Does initializing a data member and not referencing/using it further take up memory during runtime, or does the compiler simply ignore that member? struct Foo { int var1; int var2; Foo() : var1{5} { std::cout << var1; …
Chriss555888
  • 863
  • 1
  • 6
  • 10
96
votes
15 answers

How to convert a structure to a byte array in C#?

How do I convert a structure to a byte array in C#? I have defined a structure like this: public struct CIFSPacket { public uint protocolIdentifier; //The value must be "0xFF+'SMB'". public byte command; public byte errorClass; …
Swapnil Gupta
  • 8,751
  • 15
  • 57
  • 75
94
votes
8 answers

What does "request for member '*******' in something not a structure or union" mean?

Is there an easy explanation for what this error means? request for member '*******' in something not a structure or union I've encountered it several times in the time that I've been learning C, but I haven't got a clue as to what it means.
Pieter
  • 31,619
  • 76
  • 167
  • 242