struct

Method `mul` has an incompatible type for trait

前提是你 提交于 2020-05-17 05:11:09
问题 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

Difference between memory allocations of struct member (pointer vs. array) in C

孤街浪徒 提交于 2020-05-16 19:10:50
问题 Is there any effective difference between these two styles of allocating memory? 1. typedef struct { uint8_t *buffer; } Container; Container* init() { Container* container = calloc(sizeof(Container), 1); container->buffer = calloc(4, 1); return container; } 2. typedef struct { uint8_t buffer[4]; } Container; Container* init() { Container* container = calloc(sizeof(Container), 1); return container; } As far as I understand, the whole Container struct will be heap allocated and buffer will

Python struct giving incorrect length

人盡茶涼 提交于 2020-05-16 03:08:08
问题 Couple of issues with python struct. Please let me know what is correct. Document mentions length of l/L as 4 but when checked with calcsize it gives 8. >>> struct.calcsize('l') 8 struct module calcsize is giving wrong size. If individual element size is calculated, it's sum is 90 but when calculated together with calcsize it gives 92. >>> struct.calcsize('8s2sIII30s32s6s') 92 >>> struct.calcsize('8s') 8 >>> struct.calcsize('2s') 2 >>> struct.calcsize('III') 12 >>> struct.calcsize('30s') 30 >

Is multiple-level “struct inheritance” guaranteed to work everywhere?

社会主义新天地 提交于 2020-05-15 06:20:46
问题 I know that in C, the first member of a struct is guaranteed to have no padding before it. Thus &mystruct == &mystruct.firstmember is always true. This allows the "struct inheritance" technique, as described in this question: typedef struct { // base members } Base; typedef struct { Base base; // derived members } Derived; // ... later Base* object = (Base*) malloc(sizeof()); // This is legal However, I'd like to make sure that this actually works safely with unlimited layers of "inheritance"

Why must the 'struct' keyword precede struct instances in C?

≯℡__Kan透↙ 提交于 2020-05-15 05:01:08
问题 Let's say I define a struct in C. If I declare an instance of that struct, it's necessary that I include the 'struct' keyword in front of it. // Define struct struct Book { char title[50]; char author[50]; char subject[100]; int book_id; }; // Instantiate struct int main() { struct Book myBook; } My question: why is it necessary for the struct keyword to precede any instantiation of the struct? It seems like the compiler would have plenty of information to deduct that 'Book' is a struct. I

Why must the 'struct' keyword precede struct instances in C?

丶灬走出姿态 提交于 2020-05-15 05:00:34
问题 Let's say I define a struct in C. If I declare an instance of that struct, it's necessary that I include the 'struct' keyword in front of it. // Define struct struct Book { char title[50]; char author[50]; char subject[100]; int book_id; }; // Instantiate struct int main() { struct Book myBook; } My question: why is it necessary for the struct keyword to precede any instantiation of the struct? It seems like the compiler would have plenty of information to deduct that 'Book' is a struct. I

typedef in c: struct or function reference?

两盒软妹~` 提交于 2020-05-14 07:49:36
问题 Analyzing some part of software written by someone else, I found the following line of code: typedef const struct rpc_method *(*super_t)(RPC*); Ok, i know, typedef rpc_method *(*super_t)(RPC*); declares a type super_t which is a function pointer... I also know what typedef struct means, but the combination of the two?? Is it a struct with a single entry??? And what means const in this context?? Could it be that const and struct are exchanged??? Nevertheless seems to compile with my gcc eabi.

typedef in c: struct or function reference?

时间秒杀一切 提交于 2020-05-14 07:45:08
问题 Analyzing some part of software written by someone else, I found the following line of code: typedef const struct rpc_method *(*super_t)(RPC*); Ok, i know, typedef rpc_method *(*super_t)(RPC*); declares a type super_t which is a function pointer... I also know what typedef struct means, but the combination of the two?? Is it a struct with a single entry??? And what means const in this context?? Could it be that const and struct are exchanged??? Nevertheless seems to compile with my gcc eabi.

typedef in c: struct or function reference?

China☆狼群 提交于 2020-05-14 07:45:04
问题 Analyzing some part of software written by someone else, I found the following line of code: typedef const struct rpc_method *(*super_t)(RPC*); Ok, i know, typedef rpc_method *(*super_t)(RPC*); declares a type super_t which is a function pointer... I also know what typedef struct means, but the combination of the two?? Is it a struct with a single entry??? And what means const in this context?? Could it be that const and struct are exchanged??? Nevertheless seems to compile with my gcc eabi.

How can I initialize View Again in SwiftUI?

﹥>﹥吖頭↗ 提交于 2020-05-13 07:28:25
问题 I’m using SwfitUI in my project and I have a NavigationView and List. I’m clicking cell after open the detail view and click navigation back button. I want to remove view (it’s struct, in SwiftUI) after click navigation back button. Because if I click the same cell or button again, it isn’t initialize new view, it’s shows old view. I want to do refresh this view. How do I do? My FirstView Struct is: struct FirstView: View { @ObservedObject var viewModel: FirstViewModel var body: some View {