struct

C# return struct reference from method

夙愿已清 提交于 2020-05-24 05:08:12
问题 In C++, returning a reference of an object allocated on the stack in a method, yields garbage values due to the fact, that the stack object is destroyed as soon the method leaves the scope. Given that in C# structs are allocated on the stack, would this yield garbage values as well? struct Test { //data } Test Foo() { Test t1 = new Test(); return t1; } 回答1: keyword struct in C# is allow to describe value type. When you return value type from method, it creates new copy of it. 回答2: I think you

How to convert JSON string to struct

谁说胖子不能爱 提交于 2020-05-23 17:57:29
问题 I have start working in golang, I am trying to parse JSON string to struct but its not working. JSON String: dailies":[{"userAccessToken":"acessToken","uploadStartTimeInSeconds":1499744832,"uploadEndTimeInSeconds":1499744832,"callbackURL":"callbackurl"}]} type pingDataFormat struct { userAccessToken string uploadStartTimeInSeconds int uploadEndTimeInSeconds int callbackURL string } Below is code which convert JSON String to structs pingJSON := make(map[string][]pingDataFormat) err := json

Initializing a Global Struct in C

若如初见. 提交于 2020-05-23 05:40:09
问题 What is the best way to accomplish the following in C? #include <stdio.h> struct A { int x; }; struct A createA(int x) { struct A a; a.x = x; return a; } struct A a = createA(42); int main(int argc, char** argv) { printf("%d\n", a.x); return 0; } When I try to compile the above code, the compiler reports the following error: "initializer element is not constant" The bad line is this one: struct A a = createA(42); Can someone explain what is wrong? I'm not very experienced in C. Thanks! 回答1:

Initializing a Global Struct in C

血红的双手。 提交于 2020-05-23 05:39:08
问题 What is the best way to accomplish the following in C? #include <stdio.h> struct A { int x; }; struct A createA(int x) { struct A a; a.x = x; return a; } struct A a = createA(42); int main(int argc, char** argv) { printf("%d\n", a.x); return 0; } When I try to compile the above code, the compiler reports the following error: "initializer element is not constant" The bad line is this one: struct A a = createA(42); Can someone explain what is wrong? I'm not very experienced in C. Thanks! 回答1:

Why does “can't leak private type” only apply to structs and not enums?

╄→гoц情女王★ 提交于 2020-05-22 08:03:11
问题 In Learning Rust With Entirely Too Many Linked Lists, they show that a pub enum can't hold a private struct :, struct Node { elem: i32, next: List, } pub enum List { Empty, More(Box<Node>), } This will cause the compiler to complain: error[E0446]: private type `Node` in public interface --> src/main.rs:8:10 | 8 | More(Box<Node>), | ^^^^^^^^^^ can't leak private type But this code will not cause an error even though Link is private: pub struct List { head: Link, } enum Link { Empty, More(Box

How to store an object inside another object without modifying the original using struct?

旧巷老猫 提交于 2020-05-17 08:53:12
问题 Contextualization: When the data structure performs queries it performs rotation transformations in the tree. In this way, the original tree that was loaded is modified. So if I make another appointment instantly, the consultation is different. I need to find a way to keep the original data structure in memory so that the query data structure is a copy of it. Remembering that the data structure in question is an object. That is, how do I copy an object without changing the original? If I try

Why the result of this code is the same when the arg is different?

心已入冬 提交于 2020-05-17 07:06:21
问题 Feel free to make this post as a duplicate if there's already an answer for it because I haven't found the answer. Here's the code (first code): #include <stdio.h> #include <stdlib.h> typedef struct { int val; } yay; yay* New (int val) { yay *Node=(yay*) malloc (sizeof (yay)); Node->val=val; return Node; } void chg (yay *lol) {lol->val=9;} int main () { yay *boi=New (5); printf ("%d\n", boi->val); chg (boi); printf ("%d\n", boi->val); return 0; } The result of the code above is: 5 9 And my

How to name an instance of a struct the contents of a variable - Swift

限于喜欢 提交于 2020-05-17 06:10:12
问题 There is almost certainly a better way of doing this and I'd love to know but I can't phrase it in a question so essentially here is my problem: I am creating an app that presents a list of items(In a table view) which have various bits of data that come along with the item(String Int Date ect). I've decided that the best way to store this data is in a struct because it allows me to store lost of different types of data as well as run processes on it. The problem is that I want to have

Method `mul` has an incompatible type for trait

亡梦爱人 提交于 2020-05-17 05:13:03
问题 I'm creating a simple matrix struct in Rust and I'm trying to implement some basic operator methods: use std::ops::Mul; struct Matrix { cols: i32, rows: i32, data: Vec<f32>, } impl Matrix { fn new(cols: i32, rows: i32, data: Vec<f32>) -> Matrix { Matrix { cols: cols, rows: rows, data: data, } } } impl Mul<f32> for Matrix { type Output = Matrix; fn mul(&self, m: f32) -> Matrix { let mut new_data = Vec::with_capacity(self.cols * self.rows); for i in 0..self.cols * self.rows { new_data[i] = self

Method `mul` has an incompatible type for trait

醉酒当歌 提交于 2020-05-17 05:12:20
问题 I'm creating a simple matrix struct in Rust and I'm trying to implement some basic operator methods: use std::ops::Mul; struct Matrix { cols: i32, rows: i32, data: Vec<f32>, } impl Matrix { fn new(cols: i32, rows: i32, data: Vec<f32>) -> Matrix { Matrix { cols: cols, rows: rows, data: data, } } } impl Mul<f32> for Matrix { type Output = Matrix; fn mul(&self, m: f32) -> Matrix { let mut new_data = Vec::with_capacity(self.cols * self.rows); for i in 0..self.cols * self.rows { new_data[i] = self