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

Is the memory allocated for struct members continguous? What if a struct member is an array?

In C/C++ suppose I define a simple struct named test as follows. struct test { double height; int age; char gender; } For a specific instance of this struct say test A are A.height, A.age, A.gender contiguous in memory? More…
smilingbuddha
  • 14,334
  • 33
  • 112
  • 189
52
votes
5 answers

Why and when to use static structures in C programming?

I have seen static structure declarations quite often in a driver code I have been asked to modify. I tried looking for information as to why structs are declared static and the motivation of doing so. Can anyone of you please help me understand…
kutty
  • 541
  • 1
  • 4
  • 4
52
votes
3 answers

Capitals in struct fields

I'm using this library to access CouchDB (cloudant to be specific) "github.com/mikebell-org/go-couchdb" and I've noticed a problem. When I go to add a file to the database and pass in a struct, only the fields of the struct which started with a…
Saif Abid
  • 539
  • 1
  • 4
  • 5
52
votes
2 answers

Force C++ structure to pack tightly

I am attempting to read in a binary file. The problem is that the creator of the file took no time to properly align data structures to their natural boundaries and everything is packed tight. This makes it difficult to read the data using C++…
steveo225
  • 11,394
  • 16
  • 62
  • 114
52
votes
4 answers

Why can I not brace initialize a struct derived from another struct?

When I run this code: struct X { int a; }; struct Y : public X {}; X x = {0}; Y Y = {0}; I get: error: could not convert ‘{0}’ from ‘’ to ‘Y’ Why does brace initialization work for the base class but not the…
Eric
  • 95,302
  • 53
  • 242
  • 374
51
votes
7 answers

What does this error mean: "error: expected specifier-qualifier-list before 'type_name'"?

I've been working on the Cell processor and I'm trying to create a struct that will hold an spe_context_ptr_t, which will be used within the thread to launch an spe context and will also hold a pointer to something else that will be passed to the…
Paul Wicks
  • 62,960
  • 55
  • 119
  • 146
51
votes
4 answers

Why can't structs be declared as const?

They are immutable value types on the stack. What keeps me from having them a…
Lazlo
  • 8,518
  • 14
  • 77
  • 116
51
votes
10 answers

What's the difference between an object and a struct in OOP?

What distinguishes and object from a struct? When and why do we use an object as opposed to a struct? How does an array differ from both, and when and why would we use an array as opposed to an object or a struct? I would like to get an idea of…
Mohamad
  • 34,731
  • 32
  • 140
  • 219
51
votes
1 answer

How to filter a vector of custom structs?

I am trying to filter a Vec where Vocabulary is a custom struct, which itself contains a struct VocabularyMetadata and a Vec: #[derive(Serialize, Deserialize)] pub struct Vocabulary { pub metadata: VocabularyMetadata, pub…
Zelphir Kaltstahl
  • 5,722
  • 10
  • 57
  • 86
51
votes
5 answers

C empty struct -- what does this mean/do?

I found this code in a header file for a device that I need to use, and although I've been doing C for years, I've never run into this: struct device { }; struct spi_device { struct device dev; }; and it used as in: int…
Billy Pilgrim
  • 1,842
  • 3
  • 22
  • 32
50
votes
6 answers

Difference between classes and namespaces?

I'm looking at namespaces and I don't really see a difference between these and classes. I'm teaching myself C++ I've gotten several books online, so I know I'm not learning the most effectively. Anyway, can someone tell me the difference between…
TimothyTech
  • 745
  • 2
  • 11
  • 18
50
votes
4 answers

How does function ACTUALLY return struct variable in C?

How does a function return value is clear to me, just to kick start: int f() { int a = 2; return a; } Now a gets the memory in stack and its life-span is within the f() in order to return the value it copies the value to a special register…
gitesh.tyagi
  • 2,271
  • 1
  • 14
  • 21
50
votes
3 answers

Is it possible to define equality for named types/structs?

After reading a related question about using slices in maps, I became curious about equality in Go. I know it's possible to override the equals method of a Java Object. Is there a similar way to define how Go checks user defined types/structs for…
Bill DeRose
  • 2,330
  • 3
  • 25
  • 36
50
votes
4 answers

Overloading operators in typedef structs (c++)

I want to make a typedef struct called pos (from position) that stores coordinates x and y. I am trying to overload some operators for this struct, but it does not compile. typedef struct { int x; int y; inline pos operator=(pos a) { …
tuket
  • 3,232
  • 1
  • 26
  • 41
49
votes
6 answers

Invalid application of sizeof to incomplete type with a struct

I have a struct where I put all the information about the players. That's my struct: struct player{ int startingCapital; int currentCapital; int startingPosition; int currentPosition; int activePlayer; int canPlay; }; And…
captain
  • 1,747
  • 5
  • 20
  • 32