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

C fread() magically reading dynamically allocated struct members, how?

This is a test program that I have written for a larger project that I am working on. It has to do with writing struct data to disk with fwrite() and then reading that data back with fread(). One member of the struct is dynamically allocated. …
tsliwkan
  • 141
  • 2
  • 10
7
votes
3 answers

Edit where a pointer points within a function

I have a pointer to a struct object in C++. node *d=head; I want to pass that pointer into a function by reference and edit where it points. I can pass it but it doesn't change where the initial pointer points but only the local pointer in the…
Theocharis K.
  • 1,281
  • 1
  • 16
  • 43
7
votes
2 answers

How to refer to recursive structs through pointers using vectors

I have structs, let's call them sn, that look like: struct sn { string name; vector connected_to; }; Now, suppose I have the connected_to vector already declared from 0 - 9; and I'm connecting sn A, to sn B: A.connected_to[0] = &B;…
Deniz
  • 1,481
  • 3
  • 16
  • 21
7
votes
2 answers

Structure definition in header file for a library and compilation differences

I have a code which is compiled into a library (dll, static library and so). I want the user of this library to use some struct to pass some data as parameters for the library function. I thought about declaring the struct in the API header file.…
MByD
  • 135,866
  • 28
  • 264
  • 277
7
votes
4 answers

C Pointer Arithmetic sizeof(struct)

Here is the code in question #include struct test { unsigned char t; unsigned short u; unsigned char v; }; int main () { struct test * a = (void *) 0x1000; printf("%x %p %p\n", sizeof(struct test), …
endeavormac
  • 659
  • 2
  • 8
  • 18
7
votes
4 answers

Accessing enum values defined in a struct

If I have the following: struct LineChartScene::LineChartSceneImpl { enum ContextMenuAction {ShowLabels, ShowPoints, SaveAsImage}; }; How can I access ShowLabels, ShowPoints etc outside LineChartScene::LineChartSceneImpl struct? I thought…
Donotalo
  • 12,748
  • 25
  • 83
  • 121
7
votes
5 answers

Static initialization of an array of structs in C

I have a question regarding the initialization of an array of structs in C. Googling showed me that a lot of people have had very similar questions, but they weren't quite identical. Essentially, I have a global array of structs of type…
JoshVarty
  • 9,066
  • 4
  • 52
  • 80
7
votes
3 answers

C++: Storing structs in a stack

I have a struct: struct Vehicle { char ad; // Arrival departure char string license; // license value int arrival; // arrival in military time }; I want to store all the values in the struct in a stack. I can store one value in the…
Nick
  • 9,285
  • 33
  • 104
  • 147
7
votes
3 answers

Is this code guaranteed by the C standard?

I have read that if you declare two structs like this: struct Node { int a, b, c; }; struct DerivedNode { struct Node base; int d, e, f; }; Then you can use pointers to them like this: struct DerivedNode myDerivedNode; struct Node…
Rag
  • 6,405
  • 2
  • 31
  • 38
7
votes
4 answers

C - Malloc and memcpy (memory management)

I'm a bit new to C and I'm having trouble understanding how memory works, especially in-built functions like memcpy. Here's a struct I'm using struct data_t { int datasize; void *data; }; And here's an auxiliary function that I'm…
PTdude
  • 71
  • 1
  • 1
  • 3
7
votes
2 answers

How to properly use `typedef` for structs in C?

I see a lot of different typedef usages in many C courses and examples. Here is the CORRECT way to do this (example from ISO/IEC C language specification draft) typedef struct tnode TNODE; struct tnode { int count; TNODE *left,…
Kamil
  • 13,363
  • 24
  • 88
  • 183
7
votes
1 answer

Why is a "tuple struct" in rust called "named tuple"

As per this Tuple structs, which are, basically, named tuples. // A tuple struct struct Pair(i32, f32); Later in the code // Instantiate a tuple struct let pair = Pair(1, 0.1); // Access the fields of a tuple struct println!("pair contains {:?}…
Valencia
  • 117
  • 1
  • 6
7
votes
1 answer

C struct with pointers initialization

When a struct that contains an array of struct pointers is instantiated, am I guaranteed that all pointers in the struct array member will be set to NULL? Here's an example struct: typedef struct mmNode { int val; int board[2][NUM_PITS+1]; …
Zahy
  • 367
  • 7
  • 18
7
votes
1 answer

Syntax in Assigning to Map of structs

struct Structure { // Structure(const char* n, int v, bool a) : name(n), value(v), awesome(a) {} const char* name; int value; bool awesome; }; std::map map; map["alpha"] = {"Alpha", 0, true}; map["beta"] = {"Beta", …
norcalli
  • 1,215
  • 2
  • 11
  • 22
7
votes
0 answers

GCC 11 gives -Wstringop-overflow when no string operation is used

Here is my code. // test.c #include #define ARRAY_SIZE 4 struct example_t { int field0; int field1; int field2; int field3; int field4; }; struct example_t func(int *in, int array[ARRAY_SIZE]) { struct example_t out; …