struct

Understanding struct.pack in python 2.7 and 3.5+

爱⌒轻易说出口 提交于 2020-05-13 05:35:40
问题 I am attempting to understand; and resolve, why the following happens: $ python >>> import struct >>> list(struct.pack('hh', *(50,50))) ['2', '\x00', '2', '\x00'] >>> exit() $ python3 >>> import struct >>> list(struct.pack('hh', *(50, 50))) [50, 0, 50, 0] I understand that hh stands for 2 shorts. I understand that struct.pack is converting the two integers (shorts) to a c style struct . But why does the output in 2.7 differ so much from 3.5? Unfortunately I am stuck with python 2.7 for right

Understanding struct.pack in python 2.7 and 3.5+

百般思念 提交于 2020-05-13 05:34:52
问题 I am attempting to understand; and resolve, why the following happens: $ python >>> import struct >>> list(struct.pack('hh', *(50,50))) ['2', '\x00', '2', '\x00'] >>> exit() $ python3 >>> import struct >>> list(struct.pack('hh', *(50, 50))) [50, 0, 50, 0] I understand that hh stands for 2 shorts. I understand that struct.pack is converting the two integers (shorts) to a c style struct . But why does the output in 2.7 differ so much from 3.5? Unfortunately I am stuck with python 2.7 for right

Why doesn't initializing a C++ struct to `= {0}` set all of its members to 0?

好久不见. 提交于 2020-05-11 10:56:27
问题 After doing a ton of testing and writing this answer (note: the downvote on it was BEFORE my total rewrite of it), I can't understand why = {0} doesn't set all members of the struct to zero! If you do this: struct data_t { int num1 = 100; int num2 = -100; int num3; int num4 = 150; }; data_t d3 = {0}; printf("d3.num1 = %i\nd3.num2 = %i\nd3.num3 = %i\nd3.num4 = %i\n\n", d3.num1, d3.num2, d3.num3, d3.num4); ...the output is: d3.num1 = 0 d3.num2 = -100 d3.num3 = 0 d3.num4 = 150 ...although I

Why sizeof of a struct is unsafe

独自空忆成欢 提交于 2020-05-11 04:25:16
问题 The MSDN clearly states For all other types, including structs, the sizeof operator can only be used in unsafe code blocks. The C# Language Specification is even more precise : The order in which members are packed into a struct is unspecified. For alignment purposes, there may be unnamed padding at the beginning of a struct, within a struct, and at the end of the struct. The contents of the bits used as padding are indeterminate. When applied to an operand that has struct type, the result is

How to initialize a struct to 0 in C++

…衆ロ難τιáo~ 提交于 2020-05-05 12:34:19
问题 Here is a related C answer that doesn't work (as a zero initializer for a struct) on C++: Initializing a struct to 0. One of the solutions presented is this: myStruct _m1 = {0}; This works fine in C, but it doesn't work in C++. :( : error: cannot initialize a member subobject of type 'myScope::MyStruct' with an rvalue of type 'int'`. How do you zero-initialize a struct in C++? Related: Initializing a struct to 0 in C: Initializing a struct to 0 Update: (an adjacent, but NOT duplicate question

What is the purpose of a field named “_” (underscore) containing an empty struct?

荒凉一梦 提交于 2020-04-29 10:38:05
问题 I've seen two pieces of Go code using this pattern: type SomeType struct{ Field1 string Field2 bool _ struct{} // <-- what is this? } Can anyone explain what this code accomplishes? 回答1: This technique enforces keyed fields when declaring a struct. For example, the struct: type SomeType struct { Field1 string Field2 bool _ struct{} } can only be declared with keyed fields: // ALLOWED: bar := SomeType{Field1: "hello", Field2: "true"} // COMPILE ERROR: foo := SomeType{"hello", true} One reason

Custom UnmarshalYAML interface for an interface and its implementations

不问归期 提交于 2020-04-17 22:03:22
问题 I implemented an interface Fruit and two implementations of it: Apple and Banana . Into objects of the two implementations I want to load data from a yaml file: capacity: 4 Apple: - name: "apple1" number: 1 - name: "apple2" number: 1 Banana: - name: "banana1" number: 2 I implemented the UnmarshalYaml interface to load data into my objects: package main import ( "errors" "gopkg.in/yaml.v3" "log" "fmt" ) type FruitBasket struct { Capacity int `yaml:"capacity"` Fruits []Fruit } func

How to create an enum dynamically in compiling time for my struct

ε祈祈猫儿з 提交于 2020-04-16 02:32:05
问题 I have this struct below struct foo { char *name; int (*validate)(u8_t *data, size_t size); u8_t value; u8_t changed; foo_id id; }; typedef struct foo foo_t; I wish I to create an array of foo_t in compiling time through defines, like this: int my_validate(u8_t *data, size_t size) {...} FOO_CREATE(my_name, my_validate, 0, 0); FOO_CREATE(my_name2, NULL, 0, 0); and in compiling time the result be: enum { MY_NAME_FOO = 0, MY_NAME2_FOO, FOO_COUNT } foo_id; static foo_t foo[FOO_COUNT] = { { .name

What is the use of Struct Tag name in C programming?

て烟熏妆下的殇ゞ 提交于 2020-04-13 05:01:43
问题 I want to know the actual use of struct tag_name in C programming. Without using the tag_name also i was getting output as with use of tag_name. I want the exact reason behind this process. For ex: //With tag_name st1 struct st1 { int x; char c;}x={100,'a'},y={70,'e'}; //Without any tag_name struct { int x; char c;}x={100,'a'},y={70,'e'}; printf("x.x= %d \t x.c= %c \n",x.x,x.c); //Output: x.x=100 x.c=a printf("y.x= %d \t y.c= %c \n",y.x,y.c); //Output: y.x=70 y.c=e 回答1: In the first case :

initialize string pointer in struct [duplicate]

耗尽温柔 提交于 2020-04-10 07:01:06
问题 This question already has answers here : How do I do a literal *int64 in Go? (2 answers) Closed 3 years ago . Go Newbie question: I am trying to init the following struct, with a default value. I know that it works if "Uri" is a string and not pointer to a string (*string). But i need this pointer for comparing two instances of the struct, where Uri would be nil if not set, e.g. when i demarshal content from a json file. But how can I initialize such a struct properly as a "static default"?