struct

Return variable struct members

大城市里の小女人 提交于 2020-02-04 09:58:28
问题 I need to return a struct with two values in it. A double value ( time ) and an uint8_t array with a variable size. I have two functions and both of them should return the same type of struct, but with different data members ( data[9], data[64] ). I've already tried to create a struct with an additional member size , but this isn't working at all. size should initialize the array with a fixed length, but the compilers says that the variable size is not defined. typedef struct Result { double

Is it possible to decode single level JSON into 2 separate models?

感情迁移 提交于 2020-02-04 01:06:09
问题 I have a JSON response that contains information about a user. { "userId": "123456789", "email": "\"some.email@some.domain.tld", "firstName": "\"foo\"", "lastName": "\"bar\"", "name": "\"foo bar", "bio": "\"boo baz\"", "age": "42" } I'd like to create 2 models, User and Profile from the same JSON, with a single request. I'd then like Profile to be a property on the User struct. At the moment, my standard struct looks like this - struct User: Codable, Equatable { var userId: String var email:

How the sizeof() function works for Structures in C?

妖精的绣舞 提交于 2020-02-03 05:16:07
问题 The Structure is defined as follows typedef struct Sample { int test; char strtest; } Sample; In Main Function, I called Sizeof the structure. sizeof(struct Sample) I've heard the return value of sizeof on structures could be incorrect. If this is the case, what should I do to get the right value? 回答1: It does return a reliable value - just not always the value you expect. In Sample structure, you are assuming a 1 byte char and 4 byte int, but you do not get a result of "5" . Because the

Callback functions using ctypes

别等时光非礼了梦想. 提交于 2020-02-02 10:59:08
问题 I have the code in C: typedef result function_callback(struct mes_t* message, void* data) struct mes_t { uint32_t field1 uint32_t field2 void* data }; function_one(&function_callback, data) The application calls the user-defined (in the function_one ) callback function function_callback . In the callback function passed field1, field2 and data parameters (data is usually equal to 0) Whether the code on a python for this example is correctly written? class mes_t(ctypes.Structure): pass mes_t.

Binary file reading/writing in C

你。 提交于 2020-01-31 18:17:11
问题 So I have an input.bin file which contains the following IK-SZH;jdl72u;John Doe;2013-03-28 11:05 IK-GRR;kat91n;Jane Doe;2013-03-21 15:41 IK-GRR;oat62f;Jane Doe;2013-03-24 08:08 What I am doing is to read it into a struct. Doing some stuff with the data. Add/Delete lines. Then i would like to write the content of the structure back to the input.bin file in the same format as above. But instead of appearing as it is above. It's like this (no spaces): IK-SZH NUL jdl72u NUL John Doe NUL NUL NUL

Writing char pointer as struct member to file issue

断了今生、忘了曾经 提交于 2020-01-30 08:50:05
问题 I have a struct member as a char * and assign it to a string literal "John" on the struct initialisation as shown below. The string prints fine with printf. However, if I write this string to a file using fwrite, in the file I read back garbage. If I use a char array instead of a char * (commented out in the struct as shown), and write it to the file, I can read back the string in the file as expected. I can't understand this. Shouldn't fwrite in both cases take the pointer and write the

C compiler error: undefined reference to function

送分小仙女□ 提交于 2020-01-30 06:35:28
问题 After I execute the exe I get this error : undefined reference to `StudentScan' error: ld returned 1 exit status| Note: I'm bad and new to coding so don't mind my bad coding please^^ Note2: I'm just messing with random functions. #include <stdio.h> #include <stdlib.h> struct student { char firstName[20]; char AverageNum[2]; }; void StudentScan(int, struct student[]); void StudentPrint(int, struct student[]); int main() { int i; int length; struct student *studentp; printf ("\nEnter the host

How to access structure in other program's memory?

六月ゝ 毕业季﹏ 提交于 2020-01-30 04:41:53
问题 I know how to import and use read/writeprocessmomory in C#. I'm working on game trainer. I need to have "direct" access to other process memory casted to struct. I can use readprocessmemory or writeprocessmemory but that would take much time to inplement for many structures. There is this structure in C++: class CRenderer { public: char unknown0[1692]; //0x0000 BYTE ID07D54FC8; //0x069C BYTE drawObjects; //0x069D BYTE drawDeferred; //0x069E BYTE drawParticles; //0x069F BYTE ID07E1CA70; /

Is there one-line syntax for constructing a struct that contains a reference to a temporary?

筅森魡賤 提交于 2020-01-30 02:52:12
问题 Consider the following invalid Rust code. There is one struct Foo that contains a reference to a second struct Bar : struct Foo<'a> { bar: &'a Bar, } impl<'a> Foo<'a> { fn new(bar: &'a Bar) -> Foo<'a> { Foo { bar } } } struct Bar { value: String, } impl Bar { fn empty() -> Bar { Bar { value: String::from("***"), } } } fn main() { let foo = Foo::new(&Bar::empty()); println!("{}", foo.bar.value); } The compiler does not like this: error[E0716]: temporary value dropped while borrowed --> src

Clarification about Bit-field ordering semantics in C

不羁的心 提交于 2020-01-29 14:42:30
问题 I have troubles understanding the exact meaning of a paragraph of C99 draft standard (N1256) about bit-fields (6.7.2.1:10): 6.7.2.1 Structure and union specifiers [...] Semantics [...] An implementation may allocate any addressable storage unit large enough to hold a bit-field. If enough space remains, a bit-field that immediately follows another bit-field in a structure shall be packed into adjacent bits of the same unit. If insufficient space remains, whether a bit-field that does not fit