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
83
votes
8 answers

Changing the value of an element in a list of structs

I have a list of structs and I want to change one element. For example : MyList.Add(new MyStruct("john"); MyList.Add(new MyStruct("peter"); Now I want to change one element: MyList[1].Name = "bob" However, whenever I try and do this I get the…
Darren
  • 933
  • 1
  • 7
  • 9
83
votes
4 answers

What is special about structs?

I know that in C we cannot return an array from a function, but a pointer to an array. But I want to know what is the special thing about structs that makes them return-able by functions even though they may contain arrays. Why is the struct…
user5619103
83
votes
9 answers

C++: Can a struct inherit from a class?

I am looking at the implementation of an API that I am using. I noticed that a struct is inheriting from a class and I paused to ponder on it... First, I didn't see in the C++ manual I studied with that a struct could inherit from another…
augustin
  • 14,373
  • 13
  • 66
  • 79
81
votes
5 answers

When to use Struct instead of Hash in Ruby?

I don't have much programming experience. But, to me, Struct seems somewhat similar to Hash. What can Struct do well? Is there anything Struct can do, that Hash cannot do? After googling, the concept of Struct is important in C, but I don't know…
TK.
  • 27,073
  • 20
  • 64
  • 72
81
votes
8 answers

C - freeing structs

Let's say I have this struct typedef struct person{ char firstName[100], surName[51] } PERSON; and I am allocating space by malloc and filling it with some values PERSON *testPerson = (PERSON*)…
user10099
  • 1,345
  • 2
  • 17
  • 23
80
votes
8 answers

Copying one structure to another

I know that I can copy the structure member by member, instead of that can I do a memcpy on structures? Is it advisable to do so? In my structure, I have a string also as member which I have to copy to another structure having the same member. How…
asir
  • 12,843
  • 8
  • 24
  • 18
80
votes
8 answers

Struct with template variables in C++

I'm playing around with templates. I'm not trying to reinvent the std::vector, I'm trying to get a grasp of templateting in C++. Can I do the following? template typedef struct{ size_t x; T *ary; }array; What I'm trying to do is a…
monkeyking
  • 6,670
  • 24
  • 61
  • 81
79
votes
6 answers

Can structs contain fields of reference types

Can structs contain fields of reference types? And if they can is this a bad practice?
James Hay
  • 12,580
  • 8
  • 44
  • 67
78
votes
16 answers

Hiding members in a C struct

I've been reading about OOP in C but I never liked how you can't have private data members like you can in C++. But then it came to my mind that you could create 2 structures. One is defined in the header file and the other is defined in the source…
Marlon
  • 19,924
  • 12
  • 70
  • 101
77
votes
6 answers

What does the keyword "new" do to a struct in C#?

In C#, Structs are managed in terms of values, and objects are in reference. From my understanding, when creating an instance of a class, the keyword new causes C# to use the class information to make the instance, as in below: class MyClass { …
KMC
  • 19,548
  • 58
  • 164
  • 253
77
votes
4 answers

How to perform struct inline initialization in C#?

What member should I implement in my arbitrary structure to make the following assignment possible: public struct MyStruct { String s; Int length; } MyStruct myStruct = new MyStruct { s = "Hello", length = 5 }; // Now, I want the following…
Yippie-Ki-Yay
  • 22,026
  • 26
  • 90
  • 148
77
votes
8 answers

Comparing two structs using ==

I am trying to compare two structs using equals (==) in C#. My struct is below: public struct CisSettings : IEquatable { public int Gain { get; private set; } public int Offset { get; private set; } public int Bright { get;…
JMK
  • 27,273
  • 52
  • 163
  • 280
76
votes
6 answers

What needs to be overridden in a struct to ensure equality operates properly?

As the title says: do I need to override the == operator? how about the .Equals() method? Anything I'm missing?
RCIX
  • 38,647
  • 50
  • 150
  • 207
76
votes
4 answers

default visibility of C++ class/struct members

In C++, why is private the default visibility for members of classes, but public for structs?
S I
  • 1,201
  • 1
  • 10
  • 14
75
votes
3 answers

JSON omitempty With time.Time Field

Trying to json Marshal a struct that contains 2 time fields. But I only want the field to come through if it has a time value. So I'm using json:",omitempty" but it's not working. What can I set the Date value to so json.Marshal will treat it like…
Joe Bergevin
  • 3,158
  • 5
  • 26
  • 34