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

Copying struct data from host to device on CUDA using cudaMemcpy

I am facing a problem in copying struct data from host to device in the CUDA architecture. Following is the code snippet. struct point { double x,y; }; int main() { point * a = (point*)malloc(sizeof(point)); a->x=10.0; …
Vikesh
  • 2,018
  • 6
  • 23
  • 33
7
votes
2 answers

Struct Attribute on Discriminated Unions

I just realized F# records are reference types and how much boxing and unboxing I have going on. I have a lot of tiny records like this: type InputParam = | RegionString of string | RegionFloat of float32 But if I try to tag it with the…
EricP
  • 502
  • 2
  • 14
7
votes
3 answers

How to declare a list/array/struct type variable in BigQuery

How can I declare a list type variable in BigQuery so that I can use it in a where clause? I have this code WITH subquery AS ( SELECT 1 AS col1 UNION ALL SELECT 2 AS col1 UNION ALL SELECT 3 AS col1 ) SELECT col1 FROM …
user147529
  • 555
  • 1
  • 7
  • 18
7
votes
2 answers

What does it mean to typedef a struct as an array?

The graph adjacency list code in my book is given by: typedef struct vertexNode //vertexNode in an AdjacencyList { VertexType data; EdgeNodeType *firstEdgeNode; } VertexNode, AdjList[MAXVEX]; AdjList adjList; # adjList is a MAXVEX-size…
7
votes
3 answers

pointer to member of struct

I am trying to write a C program. I need the address of variable "recq". Can someone pls help me figure that out? typedef struct { int recq; } dd; struct test { dd a; }; main(){ struct test *mm; mm=(struct test *)…
Pkp
  • 929
  • 6
  • 19
  • 31
7
votes
4 answers

Converting a C++ class to a C struct (and beyond)

Past few days I have been "downgrading" > 1000 filem of C++ code into C. It's been going well until now. Suddenly I'm face to face with a class... The compiler pointed out the error first in the header file: class foobar { foo mutex; public: …
Rasman
  • 5,349
  • 1
  • 25
  • 38
7
votes
4 answers

Why does C++ have both classes and structs?

Possible Duplicate: What are the differences between struct and class in C++ If the only difference between structs and classes is the default access specifier(in C++), then why does C++ also have classes?
Lockhead
  • 2,423
  • 7
  • 35
  • 48
7
votes
1 answer

What does "Invalid managed/unmanaged type combination." mean?

I have the following struct: [StructLayout(LayoutKind.Auto,Pack=0)] private unsafe struct BIRDSYSTEMCONFIG { public byte bySystemStatus; public byte byError; public byte byNumDevices; public byte…
Tom Wright
  • 11,278
  • 15
  • 74
  • 148
7
votes
2 answers

Assignment of a struct value to this keyword

I was recently looking into internals of CancellationToken structure and discovered a bit of weird construct (to be more precise, assignment of value to this keyword). Code of one of its constructors is as following: public CancellationToken( bool…
user595010
7
votes
0 answers

Are Swift structs ALWAYS stored in the stack?

During an interview I was asked to outline the differences between structs and classes in Swift. Among my points, I made the argument that structs are stored in the stack (space for them is reserved at compile-time) whereas classes are stored in the…
Swifty
  • 839
  • 2
  • 15
  • 40
7
votes
2 answers

Spark - How to add an element to an array of structs

Having this schema: root |-- Elems: array (nullable = true) | |-- element: struct (containsNull = true) | | |-- Elem: integer (nullable = true) | | |-- Desc: string (nullable = true) How can we add a new field like that? root …
rvilla
  • 165
  • 1
  • 2
  • 10
7
votes
1 answer

how do people deal with iterating a Swift struct value-type property?

Here's an obvious situation that must arise all the time for people: struct Foundation { var columns : [Column] = [Column(), Column()] } struct Column : CustomStringConvertible { var cards = [Card]() var description : String { …
matt
  • 515,959
  • 87
  • 875
  • 1,141
7
votes
1 answer

Implementing traits without repeating methods already defined on the struct

I have a trait which only has one requirement, the presence of a methods len(&self) -> usize. It then provides implementations for a couple of new methods for the struct. trait MyTrait { fn len(&self) -> usize; // ... some new functions…
Aaron Christiansen
  • 11,584
  • 5
  • 52
  • 78
7
votes
2 answers

Empty struct in C vs empty struct in C++

Why is empty struct in C a constraint violation? Why does this rule get changed in C++? Are there any historical reasons?
Mohit Khullar
  • 245
  • 2
  • 6
7
votes
2 answers

Is pointer to struct a pointer to its first member?

I'm learning how to work with structs in C and wrote the following example: #include #include struct s1{ unsigned short member; }; int main() { struct s1 *s1_ptr = malloc(sizeof(*s1_ptr)); s1_ptr -> member = 10; …
St.Antario
  • 26,175
  • 41
  • 130
  • 318