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
63
votes
5 answers

Passing struct to function

I'm a new C programmer and I wanted to know how I can pass a struct through to a function. I'm getting an error and can't figure out the correct syntax to do it. Here is the code for it.... Struct: struct student{ char firstname[30]; char…
Daniel Del Core
  • 3,071
  • 13
  • 38
  • 52
62
votes
2 answers

Error: struct Type is not an expression

Using struct and a function that is supposed to print out the struct's elements, I have written this simple program: package main import "fmt" type Salutation struct { name string greeting string } func Greet(salutation Salutation) { …
Morteza R
  • 2,229
  • 4
  • 20
  • 31
62
votes
2 answers

What is the default value for C++ class members

What is the default values for members of a struct and members of a class in c++, and how do these rules differ (e.g. between classes/structs/primitives/etc) ? Are there circumstances where the rules about the default values differs ?
leeeroy
  • 11,216
  • 17
  • 52
  • 54
62
votes
3 answers

Are nested structs supported in Rust?

When I try to declare a struct inside of another struct: struct Test { struct Foo {} } The compiler complains: error: expected identifier, found keyword `struct` --> src/lib.rs:2:5 | 2 | struct Foo {} | ^^^^^^ expected identifier,…
abergmeier
  • 13,224
  • 13
  • 64
  • 120
62
votes
11 answers

Extending a struct in C

I recently came across a colleague's code that looked like this: typedef struct A { int x; }A; typedef struct B { A a; int d; }B; void fn(){ B *b; ((A*)b)->x = 10; } His explanation was that since struct A was the first member of struct…
rubndsouza
  • 1,224
  • 15
  • 23
61
votes
4 answers

What is the need of empty braces '{ }' at the end of array of structs?

I hit some c code in Linux kernel: static struct ctl_table ip_ct_sysctl_table[] = { { .procname = "ip_conntrack_max", .maxlen = sizeof(int), .mode = 0644, .proc_handler = proc_dointvec, }, //…
NK-cell
  • 1,145
  • 6
  • 19
61
votes
6 answers

C: pointer to struct in the struct definition

How can I have a pointer to the next struct in the definition of this struct: typedef struct A { int a; int b; A* next; } A; this is how I first wrote it but it does not work.
claf
  • 9,043
  • 17
  • 62
  • 79
61
votes
4 answers

Opaque C structs: various ways to declare them

I've seen both of the following two styles of declaring opaque types in C APIs. What are the various ways to declare opaque structs/pointers in C? Is there any clear advantage to using one style over the other? Option 1 // foo.h typedef struct foo *…
splicer
  • 5,344
  • 4
  • 42
  • 47
61
votes
8 answers

Read binary file into a struct

I'm trying to read binary data using C#. I have all the information about the layout of the data in the files I want to read. I'm able to read the data "chunk by chunk", i.e. getting the first 40 bytes of data converting it to a string, get the next…
Robert Höglund
  • 10,010
  • 13
  • 53
  • 70
61
votes
13 answers

C# Static class vs struct for predefined strings

A co-worker just created the following construction in C# (the example code is simplified). His goal was to shorten the notation for all predefined strings in the rest of the code. public struct PredefinedStrings { public const string…
Rob van Groenewoud
  • 1,824
  • 1
  • 13
  • 17
60
votes
3 answers

forward declaration of a struct in C?

#include struct context; struct funcptrs{ void (*func0)(context *ctx); void (*func1)(void); }; struct context{ funcptrs fps; }; void func1 (void) { printf( "1\n" ); } void func0 (context *ctx) { printf( "0\n" ); } void…
user1128265
  • 2,891
  • 10
  • 29
  • 34
60
votes
4 answers

Does using public readonly fields for immutable structs work?

Is this a proper way to declare immutable structs? public struct Pair { public readonly int x; public readonly int y; // Constructor and stuff } I can't think of why this would run into problems, but I just wanted to ask to make…
Mike
  • 1,037
  • 1
  • 8
  • 13
60
votes
6 answers

Why use address of first element of struct, rather than struct itself?

I've just come upon yet another code base at work where developers consistently use the address of the first element of structs when copying/comparing/setting, rather than the struct itself. Here's a simple example. First there's a struct…
Magnus
  • 4,644
  • 1
  • 33
  • 49
60
votes
3 answers

Structure of Arrays vs Array of Structures

From some comments that I have read in here, it is preferable to have Structure of Arrays (SoA) over Array of Structures (AoS) for parallel implementations like CUDA. If that is true, can anyone explain why?
BugShotGG
  • 5,008
  • 8
  • 47
  • 63
59
votes
4 answers

How does "public private(set)" access modifiers work?

So I'm going through the Apple docs here - Apple Docs Then I ran into this: public struct TrackedString { public private(set) var numberOfEdits = 0 public var value: String = "" { didSet { numberOfEdits += 1 } …
brkr
  • 1,208
  • 1
  • 12
  • 18