struct

Looking for an easy way to reinitialize a struct

末鹿安然 提交于 2020-01-11 12:39:20
问题 I have a struct called CoolStruct: struct CoolStruct { int id; uint32 type; uint32 subtype; String name; }; I have a vector of these structs as well: std::vector<CoolStruct> coolVector; I want to create a bunch of structs which have predefined values to push_back into this coolVector. I'd like to keep the code from getting cludgy and ugly. I would really like to keep this notation: CoolStruct t = {1, EQData::EQ_EFFECT_TYPE_PARAMETRIC, 0, T("Parametric")}; coolVector.push_back(t); CoolStruct t

Extending a structure

岁酱吖の 提交于 2020-01-11 11:30:20
问题 typedef struct A { int x; }A; typedef struct B { A a; int d; }B; void fn() { B *b; ((A*)b)->x = 10; } I read the above code snippet in SO. ((A*)b)->x is not good programming style. b->a.x is good programming style. Because anybody adds something before the statement "A a;" in structure b , it will not work. I don't understand why? I tried it too. Any suggestions please? 回答1: That trick is used to emulate inheritance in C. It makes possible to pass address A or B to function which expects

How do I correctly print a struct?

若如初见. 提交于 2020-01-11 10:57:47
问题 I'm trying to store an array of store structs within my users struct, but I can't get this to print correctly. struct users { var name: String = "" var stores: [store] } struct store { var name: String = "" var clothingSizes = [String : String]() } var myFirstStore = store(name: "H&M", clothingSizes: ["Shorts" : "Small"]) var mySecondStore = store(name: "D&G", clothingSizes: ["Blouse" : "Medium"]) var me = users(name: "Me", stores: [myFirstStore, mySecondStore]) println(me.stores) 回答1: You’re

Using -> to access property of struct but getting compiler error telling me to use ->?

元气小坏坏 提交于 2020-01-11 10:32:23
问题 This is my first time using C, it's not gentle. I'm trying to do something familiar to me in other languages, but this pointer issue is hitting me hard. I'm given the error message: recordFunctions.c:178:20: error: ‘*firstRecord’ is a pointer; did you mean to use ‘->’? firstRecord->accountno = 1; ^~ -> recordFunctions.c:179:27: error: ‘*firstRecord’ is a pointer; did you mean to use ‘->’? strcpy(firstRecord->name, name); ^~ -> recordFunctions.c:180:27: error: ‘*firstRecord’ is a pointer; did

type namespace in C

风格不统一 提交于 2020-01-11 09:36:13
问题 I've read in SO about different namespaces in C where the type are defined, e.g. there is a namespace for Structs and Unions and a namespace for typedefs. Is namespace the exact name for this? How many namespaces exist in C? 回答1: see 6.2.3 from http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf 6.2.3 Name spaces of identifiers If more than one declaration of a particular identifier is visible at any point in a translation unit, the syntactic context disambiguates uses that refer to

Struct has different size if the field order is different

时光总嘲笑我的痴心妄想 提交于 2020-01-11 09:35:30
问题 package main import ( "fmt" "unsafe" ) type A struct { a bool b int64 c int } type B struct { b int64 a bool c int } type C struct { } func main() { // output 24 fmt.Println(unsafe.Sizeof(A{})) // output 16 fmt.Println(unsafe.Sizeof(B{})) // output 0 fmt.Println(unsafe.Sizeof(C{})) } Struct A and B have the same fields, but if specified in different order they result in different size. why? Size of struct C is zero. How much memory is allocated by the system for a := C{} ? Thanks. 回答1: 1.

What are the differences between struct and class in C++?

a 夏天 提交于 2020-01-11 07:51:10
问题 This question was already asked in the context of C#/.Net. Now I'd like to learn the differences between a struct and a class in C++. Please discuss the technical differences as well as reasons for choosing one or the other in OO design. I'll start with an obvious difference: If you don't specify public: or private: , members of a struct are public by default; members of a class are private by default. I'm sure there are other differences to be found in the obscure corners of the C++

How does gcc calculate the required space for a structure?

孤者浪人 提交于 2020-01-11 05:45:10
问题 struct { integer a; struct c b; ... } In general how does gcc calculate the required space? Is there anyone here who has ever peeked into the internals? 回答1: I have not "peeked at the internals", but it's pretty clear, and any sane compiler will do it exactly the same way. The process goes like: Begin with size 0. For each element, round size up to the next multiple of the alignment for that element, then add the size of that element. Finally, round size up to the least common multiple of the

C: Recommended style for dynamically sized structs

别等时光非礼了梦想. 提交于 2020-01-10 14:19:08
问题 I need to transfer packets through the internet whose length should be dynamic. struct packet { int id; int filename_len; char filename[]; }; The problem is that zero-length arrays are not ISO-compliant. Should I use char filename[1]; instead? But then sizeof(struct packet) will not return the correct value anymore. 回答1: Classic issue. You can simply deal with it (and note that sizeof(foo) may be off by more than one if the compiler rounds the structure size up, which is (I believe) allowed),

What is the reason for error while returning a structure in this C program?

心不动则不痛 提交于 2020-01-10 05:35:08
问题 My program intends to achieve this (A) Write a C function named larger() that returns the later date of any two dates passed to it. For example, if the dates 10/9/2001 and 11/3/2001 are passed to larger() , the second date would be returned. (B) Create the larger() function that was written for (A) in a complete unit. Store the date structure returned by larger() in a separate date structure and display the member values of the returned data structure. I am working on this problem for my C