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
1 answer

How to add an XML attribute to an element in Go?

I am using the encoding/xml package in Go and the Encoder example code. While I am able to produce workable XML, I am unable to add all of the attributes that I need. As an example, let us use the concept of temperature reporting. What I need is…
Xevious
  • 407
  • 4
  • 11
7
votes
1 answer

C# Struct Generic Constructor

Using this code: struct Foo { public T1 Item1 { get; private set; } public Foo(T1 item1) { Item1 = item1; } } I encounter this error: Backing field for automatically implemented property 'Foo.Item1' must be fully…
tdashroy
  • 1,405
  • 1
  • 19
  • 18
7
votes
2 answers

Why is size_of::() not equal to the sum of the sizes of its fields?

I tried to measure the size of a struct and its fields (Playground): use std::mem; struct MyStruct { foo: u8, bar: char, } println!("MyStruct: {}", mem::size_of::()); let obj = MyStruct { foo: 0, bar: '0' }; println!("obj: …
Lukas Kalbertodt
  • 79,749
  • 26
  • 255
  • 305
7
votes
8 answers

Array of structs and new / delete

I have a struct like this: class Items { private: struct item { unsigned int a, b, c; }; item* items[MAX_ITEMS]; } Say I wanted to 'delete' an item, like so: items[5] = NULL; And I created a new item on that same spot…
Daniel Sloof
  • 12,568
  • 14
  • 72
  • 106
7
votes
3 answers

Structure tag and name, why does a local variable declared as name compile?

In some code I saw recently there was a structure defined like this: typedef struct tagMyStruct { int numberOne; int numberTwo; } MYSTRUCT; The way I understand this, tagMyStruct is the new data type and MYSTRUCT is a variable that is…
bastibe
  • 16,551
  • 28
  • 95
  • 126
7
votes
1 answer

How come C# can handle this obviously idiotic object promotion during run time?

I like the C# language very much. I'm just playing around, and would never use the code below in production code. Obviously the compiler is fooled by the layout of the struct. But how come, that the string on the Super class can still be written and…
Mike de Klerk
  • 11,906
  • 8
  • 54
  • 76
7
votes
0 answers

What are the performance pros and cons for using mutation in a struct in Swift

I have been using some functional program to avoid mutating a struct and there are no clear explanation which approach is best in terms of performance. Can anyone help out and suggest what is the best solution for in terms of performance and memory…
Tal Zion
  • 6,308
  • 3
  • 50
  • 73
7
votes
1 answer

How to create new instance of a Q_GADGET struct in QML?

I can emit signals with structs tagged with Q_GADGET from C++ to QML. Is it possible send such a struct from QML to a C++ slot? My code fails on the first step: creating an instance in QML. This code fails on the first line ... var bs = new…
pixelgrease
  • 1,940
  • 23
  • 26
7
votes
2 answers

How do I convert a C# byte array into structured data?

I am passing 64 byte data packets over USB to a microcontroller. In the microcontroller C code the packets have the structure, typedef union { unsigned char data[CMD_SIZE]; cmd_get_t get; // plus more union options } cmd_t; with typedef…
Duncan Drennan
  • 871
  • 2
  • 9
  • 21
7
votes
3 answers

Differences between user created structs and framework structs in .NET

Why C# compiler does not allow you to compile this: int a; Console.WriteLine(a); but does allow you to compile: MyStruct a; Console.WriteLine(a); where MyStruct is defined as: struct MyStruct { } Update: in the firsts case the error is: Error 1…
mgamer
  • 13,580
  • 25
  • 87
  • 145
7
votes
1 answer

Why can I use an as-of-yet undeclared member variable in a member function?

For example: struct X{ X():a{10} {} void foo() { a = 10; } private: int a; }; Why does this compile when variable a hasn't been declared yet?
ozma
  • 349
  • 1
  • 3
  • 10
7
votes
1 answer

Why can't I append to a slice that's the property of a struct in golang?

I'm trying to append a value to a golang slice, the code works if it's called in the first method, but if this method calls another method, the code seems to fail. Examples (Test3 is what I was originally trying to do): package main import ( …
Thom Seddon
  • 1,485
  • 2
  • 15
  • 25
7
votes
2 answers

Is it legal to use memcpy with a destination structure with constant members?

For example, is the following function legal: struct two_int { const int a, b; } void copy_two(const two_int *src, two_int *dest) { memcpy(dest, src, sizeof(two_int)); } It seems like at least some types of modifications of constant-defined…
SODIMM
  • 303
  • 2
  • 12
7
votes
1 answer

Does this C inheritance implementation contain undefined behavior?

struct parent { char a; char b; }; struct child { struct parent parent; int c; char d; }; struct grandchild { struct child child; long e; }; void print_parent_val(struct parent *p) { printf("%d\n", p->a); } int…
Dellowar
  • 3,160
  • 1
  • 18
  • 37
7
votes
6 answers

Vector of Structures

I have some code given to me by another person in which we have a structure struct Pair { string s1; string s2; bool equivalent; }; Then he sets up a vector of these structs hard coded std::vector PairID; staticdata() { …
Robert
  • 181
  • 1
  • 3
  • 9
1 2 3
99
100