struct

What does “Invalid managed/unmanaged type combination.” mean?

主宰稳场 提交于 2019-12-29 08:41:17
问题 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 byNumServers; public byte byXmtrNum; public ushort wXtalSpeed; public double dMeasurementRate; public byte byChassisNum; public byte byNumChassisDevices; public byte byFirstDeviceNum; public ushort wSoftwareRev; public fixed byte byFlockStatus[127]; } Based on the C++ struct: typedef struct

Strict aliasing and overlay inheritance

試著忘記壹切 提交于 2019-12-29 06:52:09
问题 Consider this code example: #include <stdio.h> typedef struct A A; struct A { int x; int y; }; typedef struct B B; struct B { int x; int y; int z; }; int main() { B b = {1,2,3}; A *ap = (A*)&b; *ap = (A){100,200}; //a clear http://port70.net/~nsz/c/c11/n1570.html#6.5p7 violation ap->x = 10; ap->y = 20; //lvalues of types int and int at the right addrresses, ergo correct ? printf("%d %d %d\n", b.x, b.y, b.z); } I used to think that something like casting B* to A* and using A* to manipulate the

How can I set the value of auto property backing fields in a struct constructor?

ε祈祈猫儿з 提交于 2019-12-29 06:38:11
问题 Given a struct like this: public struct SomeStruct { public SomeStruct(String stringProperty, Int32 intProperty) { this.StringProperty = stringProperty; this.IntProperty = intProperty; } public String StringProperty { get; set; } public Int32 IntProperty { get; set; } } Of course, a compiler error is generated that reads The 'this' object cannot be used before all of its fields are assigned to . Is there a way to assign values to the backing fields or the properties themselves, or do I have

Test for nil values in nested stucts

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-29 06:24:27
问题 I have a deeply nested struct in go. These are constructed by a json unmarshaller. Quite some fields in this struct are however 'omitifempty' so I end op with a struct that can have nills in various places. Example (the real thing is even deeper nested, and big: 400 lines of structs): package main import "fmt" type Foo struct { Foo string Bar *Bar } type Bar struct { Bar string Baz *Baz } type Baz struct { Baz string } func main() { f1 := Foo{Foo: "f1"} f2 := Foo{Foo: "f2", Bar: &Bar{Bar:

Why use different a different tag and typedef for a struct?

橙三吉。 提交于 2019-12-29 06:15:46
问题 In C code, I've seen the following: typedef struct SomeStructTag { // struct members } SomeStruct; I'm not clear on why this is any different from: typedef struct SomeStruct { // struct members } SomeStruct; What's the point of using a particular name for the type and typedef ing it to a different type name? 回答1: In most cases, one could use the same name for both purposes without any problem. The practice of people using different names in code probably stems from the days before ANSI

How to initialize a const variable inside a struct in C?

≯℡__Kan透↙ 提交于 2019-12-29 05:59:06
问题 I write a struct struct Tree{ struct Node *root; struct Node NIL_t; struct Node * const NIL; //sentinel } I want struct Node * const NIL = &NIL_t; I can't initialize it inside the struct. I'm using msvs. I use C, NOT C++. I know I can use initialization list in C++. How to do so in C? 回答1: If you are using C99, you can used designated initializers to do this: struct Tree t = { .root = NULL, .NIL = &t.NIL_t }; This only works in C99, though. I've tested this on gcc and it seems to work just

How to initialize a const variable inside a struct in C?

隐身守侯 提交于 2019-12-29 05:58:06
问题 I write a struct struct Tree{ struct Node *root; struct Node NIL_t; struct Node * const NIL; //sentinel } I want struct Node * const NIL = &NIL_t; I can't initialize it inside the struct. I'm using msvs. I use C, NOT C++. I know I can use initialization list in C++. How to do so in C? 回答1: If you are using C99, you can used designated initializers to do this: struct Tree t = { .root = NULL, .NIL = &t.NIL_t }; This only works in C99, though. I've tested this on gcc and it seems to work just

How to call a non static member function from a static member function without passing class instance

情到浓时终转凉″ 提交于 2019-12-29 05:37:05
问题 I need to call a non static member function from a static member function of the same class. The static function is a callback. It can receive only void as data, though which i pass a char*. So i cannot directly provide the class instance to the callback. I can pass a structure instead of char to the callback function. Can anyone give eg code to use the non static member function in a static member function . and use the structure in the static member function to use the instance of the class

Can we have a struct element of type Variable length array? [duplicate]

大憨熊 提交于 2019-12-29 05:25:12
问题 This question already has answers here : Array of variable length in struct (3 answers) Closed last year . Can we declare a structure element of variable length? The condition is as follows: typedef struct { uint8_t No_Of_Employees; uint8_t Employee_Names[No_Of_Employees][15]; }st_employees; 回答1: If coding in C99 or C11, you might want to use flexible array members (you don't give an explicit dimension, but you should have a convention about it at runtime in your head). typedef struct {

How do I dump the struct into the byte array without reflection?

一个人想着一个人 提交于 2019-12-29 03:27:28
问题 I already found encoding/binary package to deal with it, but it depended on reflect package so it didn't work with uncapitalized(that is, unexported) struct fields. However I spent a week to find that problem out, I still have a question: if struct fields should not be exported, how do I dump them easily into binary data? EDIT: Here's the example. If you capitalize the name of fields of Data struct, that works properly. But Data struct was intended to be an abstract type, so I don't want to