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

What (exactly) does the type keyword do in go?

I've been reading A Tour of Go to learn Go-Lang and so far it's going good. I'm currently on the Struct Fields Lesson and here is the sample code from the right hand side: package main import "fmt" type Vertex struct { X int Y int } func…
Malekai
  • 4,765
  • 5
  • 25
  • 60
43
votes
3 answers

Struct's zero value

Here is sample code: package main import ( "fmt" ) type A struct { Name string } func (this *A) demo(tag string) { fmt.Printf("%#v\n", this) fmt.Println(tag) } func main() { var ele A ele.demo("ele are called") ele2…
soapbar
  • 2,411
  • 4
  • 18
  • 24
43
votes
1 answer

Cast a struct pointer to interface pointer in Golang

I have a function func doStuff(inout *interface{}) { ... } the purpose of this function is to be able to treat a pointer of any type as input. But when I want to call it with a the pointer of a struct I have an error. type MyStruct struct { …
taharqa
  • 1,112
  • 1
  • 13
  • 19
43
votes
6 answers

C programming: Dereferencing pointer to incomplete type error

I have a struct defined as: struct { char name[32]; int size; int start; int popularity; } stasher_file; and an array of pointers to those structs: struct stasher_file *files[TOTAL_STORAGE_SIZE]; In my code, I'm making a pointer to the…
confusedKid
  • 3,231
  • 6
  • 30
  • 49
43
votes
4 answers

convert struct pointer to interface{}

If I have: type foo struct{ } func bar(baz interface{}) { } The above are set in stone - I can't change foo or bar. Additionally, baz must converted back to a foo struct pointer inside bar. How do I cast &foo{} to interface{} so I can…
lf215
  • 1,185
  • 7
  • 41
  • 83
43
votes
5 answers

How to return a struct from a function in C++?

I've tried on a few different forums and can't seem to get a straight answer, how can I make this function return the struct? If I try 'return newStudent;' I get the error 'No suitable user-defined conversion from studentType to studentType…
tail_recursion
  • 660
  • 1
  • 7
  • 11
43
votes
4 answers

Passing structs to functions

I am having trouble understanding how to pass in a struct (by reference) to a function so that the struct's member functions can be populated. So far I have written: bool data(struct *sampleData) { } int main(int argc, char *argv[]) { …
Phorce
  • 2,632
  • 13
  • 43
  • 76
42
votes
4 answers

struct serialization in C and transfer over MPI

I have defined a custom struct which I need to send over to another MPI process using the MPI_Bsend (or MPI_Send). Here is the struct: struct car{ int shifts; int topSpeed; }myCar; The issue is that apart from primitive types MPI doesn't seem…
stratis
  • 7,750
  • 13
  • 53
  • 94
42
votes
4 answers

Efficiently mapping one-to-many many-to-many database to struct in Golang

Question When dealing with a one-to-many or many-to-many SQL relationship in Golang, what is the best (efficient, recommended, "Go-like") way of mapping the rows to a struct? Taking the example setup below I have tried to detail some approaches with…
Ewan
  • 14,592
  • 6
  • 48
  • 62
42
votes
7 answers

Cannot call value of non-function type 'String'

I'm trying to pass the ILTItem variable into my ILTViewController, triggered by AppDelegate.swift when the user launches my app via a deeplink. The code I have errors with: Cannot call value of non-function type 'String' on the line where I define…
James
  • 1,088
  • 3
  • 11
  • 29
42
votes
3 answers

Is this C++ destructor redundant?

I've received some C++ code with various structures defined like this: typedef struct _someStruct_ { std::string someString; std::vector someVectorOfStrings; int someOtherStuff; ~_someStruct_() { …
Simon
  • 453
  • 4
  • 6
42
votes
6 answers

Finding offset of a structure element in c

struct a { struct b { int i; float j; }x; struct c { int k; float l; }y; }z; Can anybody explain me how to find the offset of int k so that we can find the address of int i?
MSB
  • 453
  • 1
  • 4
  • 6
42
votes
21 answers

Structs - real life examples?

There are any number of questions here on SO dealing with the differences between Structs and Classes in C#, and when to use one or the other. (The one sentence answer: use structs if you need value semantics.) There are plenty of guidelines out…
Electrons_Ahoy
  • 36,743
  • 36
  • 104
  • 127
41
votes
8 answers

Should I use a struct or a class to represent a Lat/Lng coordinate?

I am working a with a geo-coding API and need to represent the coordinate of a returned point as a Latitude / Longitude pair. However, I am unsure whether to use a struct or a class for this. My initial thought was to use a struct, but they seem to…
Dan Diplo
  • 25,076
  • 4
  • 67
  • 89
41
votes
3 answers

Method invocation on a struct?

When we invoke a method on a object, then the reference of the object is passed implicitly to the method. So my question is what happens when a method is invoked on a struct ? Is it similar to classes in this aspect?
Searock
  • 6,278
  • 11
  • 62
  • 98