unions

Declaring C struct with union on C#

浪尽此生 提交于 2019-12-11 10:12:43
问题 I want to declare _WAITCHAIN_NODE_INFO struct declaration in my code - for WCT usage. I've tried to follow the tutorials from : https://msdn.microsoft.com/en-us/library/eshywdt7(v=vs.110).aspx But every time then I use WCT call with my managed struct declaration I get heap corruption. typedef struct _WAITCHAIN_NODE_INFO { WCT_OBJECT_TYPE ObjectType; WCT_OBJECT_STATUS ObjectStatus; union { struct { WCHAR ObjectName[WCT_OBJNAME_LENGTH]; LARGE_INTEGER Timeout; BOOL Alertable; } LockObject;

Why this union is deleting the 1st records in arrays in the c code?

吃可爱长大的小学妹 提交于 2019-12-11 07:45:58
问题 Here is one of my header file which consists of a union template with 4 different structures. #define MAX 3 union family { struct name /*for taking the name and gender of original member*/ { unsigned char *namess; unsigned int gender; union family *ptr_ancestor; /*this is a pointer to his ancestors details*/ }names; struct male /*for taking the above person's 3 male ancestors details if he is male*/ { unsigned char husb_names[3][20]; unsigned char wife_names[3][20]; unsigned int wife_status[3

Proper way to access array members as a different type?

依然范特西╮ 提交于 2019-12-11 07:19:10
问题 I have a large array of uint16_t . Most of its members are uint16_t , but some are int16_t and some uint8_t . How would you handle that? By the way, I tried: Pointers: Used 2 pointers, one int16_t* and the other uint8_t* , both initialized to the start of the array, to access member of the array that are int16_t and uint8_t . (That worked initially, but I ran into problems when later in the program something else changed the value of the pointers, so I don't trust it.) Type definition with a

Casting structure pointer to union pointer

混江龙づ霸主 提交于 2019-12-11 05:25:37
问题 I have a union containing various compatible struct members: enum Type { T1, T2, … }; struct S1 { enum Type type; … }; struct S2 { enum Type type; … }; … union U { enum Type type; struct S1 as_s1; struct S2 as_s2; … }; The type field of these structures is treated as immutable, and only the field of the union corresponding to the current type is accessed. I am aware that casting union U * to struct S1 * is defined as long as the type invariant is upheld, but is the reverse also true? struct

A well supported alternative to c++ unions?

ぃ、小莉子 提交于 2019-12-11 04:34:38
问题 I think that unions can be perfect for what i have in mind and especially when I consider that my code should run on a really heterogeneous family of machines, especially low-powered machine, what is bugging me is the fact that the people who creates compilers doesn't seem to care too much about introducing and offering a good union support, for example this table is practical empty when it comes to Unrestricted Unions support, and this is a real unpleasant view for my projects. There are

GCC Inconsistent compilation error 'has no member named ' [duplicate]

假装没事ソ 提交于 2019-12-11 01:54:24
问题 This question already has answers here : Anonymous union within struct not in c99? (7 answers) Closed 5 years ago . In an C application, the following code is present. #include <stdlib.h> #include <string.h> typedef struct { /*! matrix ID */ int id; /*! number of rows */ int num_rows; /*! number of columns */ int num_cols; union { float *matrix; float *vector; }; } PpetKeviParams; typedef struct { char DB_char; int DB_index; float DB_val; PpetKeviParams outvec; } DBType; int main(void) {

union of value and function pointer

十年热恋 提交于 2019-12-11 00:38:03
问题 I am struggling with using unions. Why am I unable to pass the function pointer to where the union would be? Any help would be greatly appreciated. Edit: removed a typedef #include <stdio.h> union U { int(*fnPtr)(int); int i; }; enum E { OPTION_0 = 0, OPTION_1 = 1 }; int multiply_by_two (int x) { return 2 * x; } int f (int x, enum E e, union U u) { switch (e) { case OPTION_0: /* Return the sum */ return x + u.i; case OPTION_1: /* Return 2 * x */ return u.fnPtr (x); } } int main (void) { int a

Type punning between integer and array using `union`?

ⅰ亾dé卋堺 提交于 2019-12-10 18:28:35
问题 Is it legal to do a type-punning between an integer and an array of integers? Specific code: #include <nmmintrin.h> #include <stdint.h> union Uint128 { __uint128_t uu128; uint64_t uu64[2]; }; static inline uint_fast8_t popcnt_u128 (__uint128_t n) { const union Uint128 n_u = {.uu128 = n}; const uint_fast8_t cnt_a = _mm_popcnt_u64(n_u.uu64[0]); const uint_fast8_t cnt_b = _mm_popcnt_u64(n_u.uu64[1]); const uint_fast8_t cnt = cnt_a + cnt_b; return cnt; } 回答1: The type-access rules in section 6.5

Structures and Unions in C, determining size and accessing members

。_饼干妹妹 提交于 2019-12-10 18:17:20
问题 All, Here is an example on Unions which I find confusing. struct s1 { int a; char b; union { struct { char *c; long d; } long e; }var; }; Considering that char is 1 byte, int is 2 bytes and long is 4 bytes. What would be the size of the entire struct here ? Will the union size be {size of char*}+ {size of double} ? I am confused because of the struct wrapped in the union. Also, how can I access the variable d in the struct. var.d ? 回答1: With no padding, and assuming sizeof(int)==sizeof(char *

Type punning Vs Union member access

心已入冬 提交于 2019-12-10 18:17:06
问题 As per this https://stackoverflow.com/a/1812932/1814023 writing one member of union and reading another member is undefined behavior. And as per this https://stackoverflow.com/a/11640603/1814023 type punning is allowed in C99 / C11 I am bit confused on these two posts, which one is correct? Need help in understanding type punning Vs accessing the member of union. Thanks. 回答1: It's implementation-defined in C89: C89 3.3.2.3 Structure and union members With one exception, if a member of a union