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

C++ struct template

I tried to use templates and structs, but they don't work. I searched a lot, but I can't find the solution. #include using namespace std; template int add(S s) { return s.num + s.num2; } int main() { struct { …
Hard Rain
  • 1,380
  • 4
  • 14
  • 19
8
votes
1 answer

How to properly use cin.peek()

This function is supposed to read a fraction and place it in an array. If the user enters '0' the function is supposed to exit. I am trying to do this using the cin.peek() function but execution always goes into the if statement and doesn't allow…
Zzz
  • 2,927
  • 5
  • 36
  • 58
8
votes
4 answers

Casting NULL to a struct pointer in C?

Is there any benefit of casting NULL to a struct pointer in C ? For example: typedef struct List { .... } List; List *listPtr = ((List *) NULL) ; Example from PostgreSQL source: #define NIL ((List *)…
metdos
  • 13,411
  • 17
  • 77
  • 120
8
votes
1 answer

How to wrap C structs in Cython for use in Python?

For a bit of learning experience, I'm trying to wrap a few parts of SDL (1.2.14) in Cython in an extension for Python 3.2. I am having a problem figuring out how to wrap C structs straight into Python, being able to access its attributes directly…
l0rdx3nu
  • 81
  • 1
  • 2
8
votes
1 answer

How do I get SWIG to automatically wrap an emulated "this" pointer to a C struct?

I've got a simple C "class" I have implemented, using function pointers in a struct to implement the member functions, and passing a pointer to the struct as the first argument to each function, similar to the implicit "this" pointer in C++. %module…
Michael
  • 9,060
  • 14
  • 61
  • 123
8
votes
2 answers

Why is a typedef not allowed in the inner struct?

I have a program that defines a typedef struct within an existing typedef struct, and I am wondering why I am getting a compilation error. Here is the program: typedef struct Outer { typedef struct Inner { int b; }INNER; …
Eight
  • 4,194
  • 5
  • 30
  • 51
8
votes
7 answers

Struct's contribution to type size

I am wondering why the following two types struct { double re[2]; }; and double re[2]; have the same size in C? Doesn't struct add a bit of size overhead?
adk
8
votes
2 answers

Will deleting a structure's pointer also delete pointers within the structure?

Assume I have a structure with two pointers each pointing to an object that has an implemented destructor. Also assume that the head points to a Listnode structure that has a non-NULL value *student and *next: struct Listnode { Student…
Pat Murray
  • 4,125
  • 7
  • 28
  • 33
8
votes
6 answers

Objective-C: How to check if a variable is an object, a struct or another primitive

I want to write a function or a directive like NSLog() that takes any kind of variable, primitives and objects. In that function I want to distinguish those. I know how it works for objects: - (void)test:(id)object { if ([object…
Django
  • 545
  • 1
  • 5
  • 17
7
votes
5 answers

Get attribute by name

I have a struct definition with about 25 elements struct X { field 1; field 2; .. }; and I'm trying to fill it with some map values Map A and it appears to be very annoying to do such thing n times X->xx = A["aaa"] …
Skan
  • 71
  • 1
  • 2
7
votes
5 answers

Storing reference types in Struct

Say I had a very simple struct in c# public struct foo{ public int a{get;set;} public int b{get;set;} public int c{get;set;} public int d{get;set;} public string hello {get;set;} } I take it the above is more 'efficient'…
maxp
  • 24,209
  • 39
  • 123
  • 201
7
votes
3 answers

Passing array of struct with typedef to a function

I need help with C programming. I have the following situation: struct Product { int code; char *name; char *spec; int quantity; float price; }; typedef struct Product products[8]; products product = { {100, "Mouse",…
wiredmark
  • 1,098
  • 6
  • 26
  • 44
7
votes
6 answers

Abstract/Base struct in C++?

I'm making a chess game and I would like to have an array of pieces. If I'm correct, in Java you can have an abstract Piece class and have King or Queen extend that class. If I were to make an array of Pieces I could place a King piece somewhere…
Austin Moore
  • 1,414
  • 6
  • 20
  • 43
7
votes
2 answers

Vector of pointers to struct

Following program looks pretty OK to me. But I can not get it compiled. #include #include using namespace std; int main() { struct a { int i; int j; }; std::vector vecA; a* pA =…
Dilawar
  • 5,438
  • 9
  • 45
  • 58
7
votes
4 answers

Are "malloc(sizeof(struct a *))" and "malloc(sizeof(struct a))" the same?

This question is a continuation of Malloc call crashing, but works elsewhere I tried the following program and I found it working (i.e. not crashing - and this was mentioned in the above mentioned link too). I May be lucky to have it working but I'm…
Sangeeth Saravanaraj
  • 16,027
  • 21
  • 69
  • 98