struct

Struct initialization and new operator

牧云@^-^@ 提交于 2021-02-06 15:01:39
问题 I have two similar structs in C#, each one holds an integer, but the latter has get/set accessors implemented. Why do I have to initialize the Y struct with new operator prior to assigning the a field? Is y still a value type when I init it with new ? public struct X { public int a; } public struct Y { public int a { get; set; } } class Program { static void Main(string[] args) { X x; x.a = 1; Y y; y.a = 2; // << compile error "unused local variable" here Y y2 = new Y(); y2.a = 3; } } 回答1:

C - Setting a struct to null (incompatible types in assignment)

我只是一个虾纸丫 提交于 2021-02-06 12:50:05
问题 I have the following struct: struct elem { int number; char character; }; struct item { struct elem element; }; and the following function: void init(struct item *wrapper) { assert(wrapper != NULL); wrapper->element = NULL; } item->element = NULL yields a incompatible types in assignment . Why is that? Shouldn't setting a struct to NULL be okay? 回答1: In C NULL is generally defined as the following #define NULL ((void*)0) This means that it's a pointer value. In this case your attempting to

C - Setting a struct to null (incompatible types in assignment)

耗尽温柔 提交于 2021-02-06 12:47:32
问题 I have the following struct: struct elem { int number; char character; }; struct item { struct elem element; }; and the following function: void init(struct item *wrapper) { assert(wrapper != NULL); wrapper->element = NULL; } item->element = NULL yields a incompatible types in assignment . Why is that? Shouldn't setting a struct to NULL be okay? 回答1: In C NULL is generally defined as the following #define NULL ((void*)0) This means that it's a pointer value. In this case your attempting to

Swift Struct doesn't conform to protocol Equatable?

孤者浪人 提交于 2021-02-06 09:45:06
问题 How do I make a structure conform to protocol "Equatable"? I'm using Xcode 7.3.1 struct MyStruct { var id: Int var value: String init(id: Int, value: String) { self.id = id self.value = value } var description: String { return "blablabla" } } When I use "MyStruct", Xcode shows the error: MyStruct does not conform to protocol "Equatable" Do you have an idea to make MyStruct conform to protocol? 回答1: Swift 4.1 (and above) Updated answer: Starting from Swift 4.1, all you have to is to conform to

How to set bool pointer to true in struct literal?

╄→尐↘猪︶ㄣ 提交于 2021-02-06 09:39:10
问题 I have the function below which accepts a bool pointer. I'm wondering if there is any notation which allows me to set the value of the is field to true in the struct literal; basically without to define a new identifier (i.e. var x := true ; handler{is: &x} ) package main import "fmt" func main() { fmt.Println("Hello, playground") check(handler{is: new(bool) }) } type handler struct{ is *bool } func check(is handler){} 回答1: You can do that but it's not optimal: h := handler{is: &[]bool{true}

How to change the Value of a struct with a function in Matlab?

孤街浪徒 提交于 2021-02-05 11:13:27
问题 s= struct('Hello',0,'World',0); for i = 1: 5 s_vec(i) = s; end I have definied a struct in Matlab within a script. Now i want to implement a function witch change the Value of the Parameters. For example: function s_struct = set_s (number, prop , value) s_struct(number).prop = value; But the function returns a new struct. It does not change my input struct. Where is my mistake? 回答1: I'am not sure to totally understand your question, but if you want to update a parameter in a structure, you

Unmarshal JSON Object to struct in Go - Result is empty [duplicate]

血红的双手。 提交于 2021-02-05 10:49:26
问题 This question already has answers here : My structures are not marshalling into json [duplicate] (3 answers) Closed 5 years ago . I'm trying to unmarshal a json object to struct in Go. I tried to stick to this example but I can't get it to work. The result stays empty. Code: package main import ( "encoding/json" "fmt" ) type MyObject struct { id string pubKey string } func main() { x := `{"id":"abc","pubKey":"QIDAQAB"}` fmt.Println("Input: ", x) var myObject MyObject json.Unmarshal([]byte(x),

How to parse Nested JSON Array into Codable struct

杀马特。学长 韩版系。学妹 提交于 2021-02-05 10:44:25
问题 I want to parse the JSON nested array into my decodable struct but the issue is there is no Key values how can i set into my decodable struct . I am also using SwiftJSON to parse object in my project... Here is my Decodable struct: struct PlayerData: Decodable { let playerID: Int? let platIDRef: Int? let EnrolledDateTime: String? let PlayerType: String? let LastCheckIn: Double? let SerialNo: Int? let Offered: Double? let PlayerReferralCode: Int? let PlayerStats: Double? let PlayerUpstream:

Why does this code contains colon in struct?

孤街浪徒 提交于 2021-02-05 09:31:11
问题 Please explain how this code is executing.why we has used ":" in structures.what is the use of colon in structures.what should be the output of sizeof operator. #include <stdio.h> int main() { struct bitfield { signed int a : 3; unsigned int b : 13; unsigned int c : 1; }; struct bitfield bit1 = { 2, 14, 1 }; printf("%ld", sizeof(bit1)); return 0; } 回答1: The : operator is being used for bit fields, that is, integral values that use the specified number of bits of a larger space. These may get

Create structures in loop without naming them

主宰稳场 提交于 2021-02-05 08:23:06
问题 Let's say I want to create structures in a loop. I don't want to change the values of the structure variables in each loop, I want to create new ones. However, from what I've learnt until know I have to name every new structure I make. Is there a way to do it without naming the structures? And more specifically, can I have a pointer in each structure that will point to the next one? What I wanna make is a BST and my struct is for every new node I add to the tree.That's how I've seen people do