struct

Is pointer to struct a pointer to its first member?

。_饼干妹妹 提交于 2020-01-02 06:22:14
问题 I'm learning how to work with struct s in C and wrote the following example: #include <stdio.h> #include <stdlib.h> struct s1{ unsigned short member; }; int main() { struct s1 *s1_ptr = malloc(sizeof(*s1_ptr)); s1_ptr -> member = 10; printf("Member = %d\n", *s1_ptr); // Member = 10 } QUESTION: Is it guaranteed that in all cases a pointer to a struct is a exactly the same pointer to its first element? In this particular case it works as I expected, but I'm not sure if it is guaranteed. Is

How to load hive table with map[structs] from another flat/simple hive table

蓝咒 提交于 2020-01-02 05:41:09
问题 I have 2 tables in hive having Order's and Order_Detail's (having 1:n relation and joined on order_id) which I am trying to load to a single table taking advantage of hive complex data type - map[struct]. Say ORDER has below data, Order_id total_amount customer 123 10.00 1 456 12.00 2 and ORDER_DETAILS have Order_id Order_Item_id Item_amount Item_type 123 1 5.00 A 123 2 5.00 B 456 1 6.00 A 456 2 3.00 B 456 3 3.00 C I would like to create single table ORDERS with all order columns and order

error: 'Int' is not convertible to '@lvalue Float'

≯℡__Kan透↙ 提交于 2020-01-02 05:37:25
问题 Given the following function: func greatestCommonDenominator(first: Int, second: Int) -> Int { return second == 0 ? first : greatestCommonDenominator(second, first % second) } And a struct with the following stuff in it: struct Fraction { var numerator: Int var denominator: Int func reduce() { let gcd = greatestCommonDenominator(numerator,denominator) self.numerator /= gcd self.denominator /= gcd } // stuff } I'm getting the following error: error: 'Int' is not convertible to '@lvalue Float'

Accessing the -1 element of an array in c

只谈情不闲聊 提交于 2020-01-02 04:48:09
问题 I have an array of structs, which is dynamically allocated. A pointer to this array is passed around to other functions. struct body{ char* name; double mass; // ... some more stuff }; body *bodies = malloc(Number_of_bodies*sizeof(body)); I need to know the size of the array, so I'm storing the size in one of the structs, which is in the 0th element of the array (the first struct). bodies[0].mass = (double)Number_of_bodies; I then return from the function a pointer to the 1st element of the

Can one element in struct access another element of itself in C?

白昼怎懂夜的黑 提交于 2020-01-02 04:31:13
问题 I want to declare a int num in struct S. Then the same struct should also have a array B of size num(So B will access num from it's own struct). while in a function, I can do, func(int A) { int max=A; //I could use A directly, I am just trying to explain my plan. int B[max]; } same won't work for struct as such, struct S { int num; int data[num]; //this is wrong, num is undeclared }; Is there any way I can do this? 回答1: Use a flexible array member: struct S { int num; int data[]; }; int x =

Reinitialize timeval struct

半城伤御伤魂 提交于 2020-01-02 02:38:40
问题 How can I reinitialize a timeval struct from time.h? I recognize that I can reset both of the members of the struct to zero, but is there some other method I am overlooking? 回答1: The completely correct and portable (albeit C99) way to zero-initialize arbitrary (possibly aggregate) types: myTime = (struct timeval){0}; This works even for structures that contain pointers and floating point members, even if the implementation does not use all-zero-bits as the representations for null pointers

How to store structs of different types without boxing

▼魔方 西西 提交于 2020-01-02 01:41:11
问题 I'm creating a messaging system for use in an XNA game. My Message types are structs because I want them to behave in a Value Type way. struct MyMessageType1 : IMessage {} struct MyMessageType2 : IMessage {} List<IMessage> messageQueue = new List<IMessage>(); I want to be able to store Messages of different types in my message queue, but I want to do so without any of them being boxed. If I have the structs implement an interface such as IMessage and I try to store them in a List, they get

How in Swift to know that struct is deleted from Memory?

为君一笑 提交于 2020-01-02 01:18:06
问题 In swift class type has method deinit() in which we can define that instance of class will be removed from memory. How we can know for struct that it will be removed from memory? For example, struct Vehicle { ... } var v: Vehicle? = Vehicle() v = nil 回答1: A simple way is the using of a dummy class. Just create an empty class and implement there the deinit(). Then use this class in your struct as member, p.e. let dummyClass = DummyClass() Once the structure is released, the deinit() function

What's the size of this C# struct?

冷暖自知 提交于 2020-01-02 01:12:08
问题 Is it 12 bytes or 16 bytes when stored in a List<DataPoint> ? public struct DataPoint { DateTime time_utc; float value; } Is there any sizeof function in C#? 回答1: Take a look at @Hans Passant's answer here for interesting background on this issue, esp. with regard to the limitations of Marshal.Sizeof . 回答2: Marshal.SizeOf() http://msdn.microsoft.com/en-us/library/y3ybkfb3.aspx 回答3: The CLR is free to lay out types in memory as it sees fit. So it's not possible to directly give "the" size.

When should a type be a struct containing another type and when should it just “extend”(?) that type?

会有一股神秘感。 提交于 2020-01-01 17:05:28
问题 I'm currently learning Go by doing the rosalind problems (basically a bunch of bioinformatics related code katas). I'm currently representing a DNA strand with a type: type DNAStrand struct { dna byte[] } My initial reason was to encapsulate the byte slice so I would know it only contained bytes representing the nucleotides: 'A', 'C', 'G', 'T' . I realized that this was obviously not guarateed since I could simply do: DNAStrand{[]byte("foo bar")} And there is no longer any guarantee that my