struct

ISO C forbids empty initializer braces in C

烈酒焚心 提交于 2019-12-23 19:23:36
问题 I have a struct like this: typedef struct { int a; int b; int c; int d; } Hello; then I declare it in this way: Hello hello[6] = {}; Then I got this warning: ISO C forbids empty initializer braces, anyhow I think I need to initialize it, how to do it in the right way? 回答1: Hello hello[6] = {{0}}; Will initialize all members of each struct to 0. 回答2: That's not valid C. The universal zero initializer in C is {0} , not {} . 回答3: Try something like this:- Hello hello[6] = {{0}}; This will

C++ Reading text file with delimiter into struct array

非 Y 不嫁゛ 提交于 2019-12-23 16:32:25
问题 I am trying to read data from a text file formatted similarly to this: knife, object, 0 bag, object, 15 kitchen, room, 400 Into an array composed of structures. Here is what I have so far, but it only reads the first element then returns garbage. #include <iostream> #include <fstream> #include <string> using namespace std; struct itemlist { string type; string selltype; int price; int diditsell=0; }; int main() { string filename; cout << "Please enter a file name. " << endl; cin >> filename;

How does padding of structs inside unions work?

不想你离开。 提交于 2019-12-23 13:13:43
问题 I have the following: #include <stdio.h> typedef union u_data { struct { int a; int b; int c; }; int elem[3]; } my_data; int main(void) { my_data data; data.a = 3; data.b = 5; data.c = -3; printf("%d, %d, %d\n", data.elem[0], data.elem[1], data.elem[2]); } and it works as I expected with output: 3, 5, -3 however I understand that structs can have padding in them so does that mean that the elements in the struct might not always align with the array? 回答1: First of all, there is a special rule

Packing bools with bit field (C++)

隐身守侯 提交于 2019-12-23 13:02:55
问题 I'm trying to interface with Ada code using C++, so I'm defining a struct using bit fields, so that all the data is in the same place in both languages. The following is not precisely what I'm doing, but outlines the problem. The following is also a console application in VS2008, but that's not super relevant. using namespace System; int main() { int array1[2] = {0, 0}; int *array2 = new int[2](); array2[0] = 0; array2[1] = 0; #pragma pack(1) struct testStruct { // Word 0 (desired) unsigned a

C error:format '%s' expects argument of type 'char *'but argument 2 has type 'char (*)[100]'

ⅰ亾dé卋堺 提交于 2019-12-23 12:53:53
问题 I'm working on an exercise in c the last few days and I'm having this warning (as the title suggests). I've tried a bunch of stuff but I don't really know how to exactly fix this. I'm not good at programming so there are mistakes. Below are the structs I'm using (which cannot be changed because that's how they are given): typedef struct bookR* book; struct bookR{ char author[MAXSTRING]; enum genres{fiction,scientific,politics}; int id; char review[MAXLINES][MAXSTRING]; }; typedef struct nodeR

Update field in struct-like enum variant

蹲街弑〆低调 提交于 2019-12-23 12:37:17
问题 I am able to use the struct update syntax with a single struct, but I am not able to use it with a struct-like enum variant. Neither can I update a field from a struct-like enum variant with the dot syntax. For instance: enum Enum { Struct { field1: i32, field2: i32, } } fn main() { let mut my_enum = Enum::Struct { field1: 1, field2: 2, }; my_enum = Enum::Struct { field1: 1, .. my_enum }; my_enum = match my_enum { strct@Enum::Struct { field1, field2 } => Enum::Struct { field1: 1, .. strct },

* is illegal for a struct?

梦想的初衷 提交于 2019-12-23 12:28:34
问题 I tried to compile the following code, but the compiler wouldn't doing because " * is illegal for a struct" is that true? struct String { int length; int capacity; unsigned check; char ptr[0]; } String; void main(){ char *s; String *new_string = malloc(sizeof(String) + 10 + 1); } 回答1: Either use a typedef: typedef struct String { int length; int capacity; unsigned check; char ptr[0]; } String; /* now String is a type */ Or explictly say struct String : void main(){ char *s; struct String *new

Struct composition with mixin and templates

别说谁变了你拦得住时间么 提交于 2019-12-23 12:16:05
问题 I can compose an AB struct that has all the members of structs A and B : template AFields() {int a;} struct A { mixin AFields; } template BFields() {int b;} struct B { mixin BFields; } struct AB { mixin AFields; mixin BFields; } A a; a.a = 1; B b; b.b = 2; AB ab; ab.a = 3; ab.b = 4; But how can I construct AB , if I don't have control over A and B and I don't have AFields and BFields ? I.e. how to write the CatStruct template so the code below compiles? struct A { int a; } struct B { int b; }

Use of unassigned local variable: value type vs custom struct

允我心安 提交于 2019-12-23 12:10:41
问题 A primitive C# value type, for example int is a struct. So, why is int not initialized? There supposed to be default constructor, I think. On the other hand, a custom struct is ok. In the following code struct STRCT { } class Program { static void Main(string[] args) { STRCT strct; strct.Equals(8); strct.GetHashCode(); strct.GetType(); strct.ToString(); int i; i.Equals(8); i.GetHashCode(); i.GetType(); i.ToString(); } } while first 5 lines of code are ok from the C# compiler view, next 5

ColdFusion Converting Form Values into a Struct

半城伤御伤魂 提交于 2019-12-23 12:10:02
问题 I am building a form for my ColdFusion application using the naming format report[{field-name}] which when using RoR or CFWheels would give me a struct on the backend called report containing all of my field names. I am using FW/1 so all of my form fields get put into the RC scope rather than remaining in the Form scope. I know that it is possible to convert my form fields into a ColdFusion struct, because, as I said, CFWheels does it. I just have no idea how to make my application do it.