struct

qsort of struct array not working

女生的网名这么多〃 提交于 2020-01-15 05:49:05
问题 I am trying to sort a struct run array called results by a char, but when I print the array, nothing is sorted. Have a look at this: struct run { char name[20], weekday[4], month[10]; (And some more...) }; typedef struct run run; int name_compare(const void *a, const void *b) { run *run1 = *(run **)a; run *run2 = *(run **)b; return strcmp(run1->name, run2->name); } int count_number_of_different_persons(run results[]) { int i = 0; qsort(results, sizeof(results) / sizeof(run), sizeof(run), name

How do I convert a struct to a byte array without a copy?

半腔热情 提交于 2020-01-15 05:45:08
问题 [StructLayout(LayoutKind.Explicit)] public struct struct1 { [FieldOffset(0)] public byte a; // 1 byte [FieldOffset(1)] public int b; // 4 bytes [FieldOffset(5)] public short c; // 2 bytes [FieldOffset(7)] public byte buffer; [FieldOffset(18)] public byte[] shaHashResult; // 20 bytes } void DoStuff() { struct1 myTest = new struct1(); myTest.shaHashResult = sha256.ComputeHash(pkBytes); // 20 bytes byte[] newParameter = myTest.ToArray() //<-- How do I convert a struct // to array without a copy?

C++ struct two dimension array into C# [duplicate]

痴心易碎 提交于 2020-01-15 03:44:29
问题 This question already has an answer here : Closed 8 years ago . Possible Duplicate: What does “Invalid managed/unmanaged type combination.” mean? how we will code these structures(Written in C++) in C# typedef struct tagBIRDMATRIX { short n[3][3]; // array of matrix elements }BIRDMATRIX; 回答1: The size should be the number of elements in your cross product. struct BIRDMATRIX { [MarshalAs(UnmanagedType.ByValArray, SizeConst = 9)] short[,] n; } 来源: https://stackoverflow.com/questions/6197504/c

Layout for struct prefix

狂风中的少年 提交于 2020-01-14 15:00:14
问题 The C/C++ languages don't reorder struct members in memory and never insert padding before the first member. But if I have 2 structures that start with the same members, can I cast between them if I only access the common members? In other words, is struct layout greedy? My concrete case is casting between VIDEOINFOHEADER and VIDEOINFOHEADER2 回答1: The C++ standard contains the following statement which, however, only applies to unions (9.2 [class.member] paragraph 19): If a standard-layout

Passing a struct with multiple entries in c++

随声附和 提交于 2020-01-14 14:55:06
问题 I am trying to pass a coordinate, which is defined as struct with 2 integer parameters (the struct is called coord) the following way: UpdateB({0,0}); where the input argument is of type coord (i.e. in the above statement I am trying to pass a coordinate 0,0 ). UpdateB is some function. I am getting an error, any ideas what the problem could be? 回答1: Make a constructor accepting two argumnets. Pass it as follows: MyFunc(Point2d(0,0)); 回答2: Pavel's got it spot on. If you want to create the

Spark Structtype for coalesce

房东的猫 提交于 2020-01-14 14:40:51
问题 I use Spark 2.0.1 Scala 2.11 How to provide a default value using coalesce for a column that's a StructType ? Say ... val ss = new StructType().add("x", IntegerType).add("y", IntegerType) val s = new StructType() .add("a", IntegerType) .add("b", ss) val d = Seq( Row(1, Row(1,2)), Row(2, Row(2,3)), Row(2, null) ) val rd = sc.parallelize(d) val df = spark.createDataFrame(rd, s) Now, df.select($"b").show results in +-----+ | b | +-----+ |[1,2]| |[2,3]| | null| +-----+ My question is how can I

C# How to set StructLayoutAttribute.Pack via Reflection?

孤人 提交于 2020-01-14 14:14:36
问题 I am creating a C# struct dynamically via reflection, and when I examine the struct's Type in my debugger I note that the StructLayoutAttribute.Pack is defaulting to 8. I would like to set the Pack to 1. Essentially, I would like to do via reflection what can be done by adding this attribute to the declaration of a struct: [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] I have tried using reflection after the type is created, but since the StructLayoutAttribute

Embedding structs in golang gives error “unknown field”

家住魔仙堡 提交于 2020-01-14 10:06:54
问题 i have a struct in user package called account type Account struct { Tp string `json:"type"bson:"type"` AccountId string `json:"account_id"bson:"account_id"` Credentials map[string]interface{} `json:"credentials,omitempty"bson:"credentials,omitempty"` ProfilePicture string `json:"profile_picture,omitempty"` Username string `json:"username"bson:"username"` AccessToken map[string]interface{}`bson:"access_token,omitempty"` } and in user/accounts im trying to embed this account struct into

Error: copy assignment operator not allowed in union

依然范特西╮ 提交于 2020-01-14 09:38:20
问题 I am compiling the code below when the following erro comes up. I am unable to find the reason. typedef union { struct { const int j; } tag; } X; int main(){ return 0; } error: member `<`anonymous union>::`<`anonymous struct> `<`anonymous union>::tag with copy assignment operator not allowed in union This code compiles fines with gcc though. Gives error only with g++. 回答1: In order to have a member of a union of some class type T , T 's special member functions (the default constructor, copy

Why is a typedef not allowed in the inner struct?

烂漫一生 提交于 2020-01-14 08:04:41
问题 I have a program that defines a typedef struct within an existing typedef struct, and I am wondering why I am getting a compilation error. Here is the program: typedef struct Outer { typedef struct Inner { int b; }INNER; INNER inner; int a; }OUTER; int main() { OUTER obj; obj.a = 10; obj.inner.b=8; return 0; } on compilation gives following error :: test.c:3:5: error:expected specifier-qualifier-list before ‘typedef’ test.c: In function ‘main’: test.c:17:5: error: ‘OUTER’ has no member named