struct

Static struct initialization in C

廉价感情. 提交于 2020-01-01 01:15:39
问题 I have a struct type as shown below: typedef struct position{ float X; float Y; float Z; float A; } position; typedef struct move{ position initial_position; double feedrate; long speed; int g_code; } move; I am trying to statically initialize it, but I have not found a way to do it. Is this possible? 回答1: It should work like this: move x = { { 1, 2, 3, 4}, 5.8, 1000, 21 }; The brace initializers for structs and arrays can be nested. 回答2: C doesn't have a notion of a static-member object of a

Struct with array of structs of unknown size

自作多情 提交于 2019-12-31 21:31:27
问题 I've been trying to wrap my head around this the whole day... Basically, I have a struct called State that has a name and another one called StateMachine with a name, an array of states and total number of states added: #include <stdio.h> #include <stdlib.h> typedef struct State { const char * name; } State; typedef struct StateMachine { const char * name; int total_states; State ** states; } StateMachine; StateMachine * create_state_machine(const char* name) { StateMachine * temp; temp =

Error: Conversion to non-scalar type requested

佐手、 提交于 2019-12-31 20:14:34
问题 I'm having a small problem trying to malloc this struct. Here is the code for the structure: typedef struct stats { int strength; int wisdom; int agility; } stats; typedef struct inventory { int n_items; char **wepons; char **armor; char **potions; char **special; } inventory; typedef struct rooms { int n_monsters; int visited; struct rooms *nentry; struct rooms *sentry; struct rooms *wentry; struct rooms *eentry; struct monster *monsters; } rooms; typedef struct monster { int difficulty;

What's the idiomatic equivalent of C structs in Lisp?

六月ゝ 毕业季﹏ 提交于 2019-12-31 18:55:33
问题 In C-type languages, there is a strong emphasis on structs/records and objects from the very beginning and in every introductory book. Then, their complete systems are designed around managing such structs, their mutual relations and inheritance. In Lisp documentation, you can usually find 1-2 pages about how Lisp "also" has a defstruct, a simple example, and thats usually it. Also, nesting of structures is never mentioned at all. For someone coming from a C background, it first seems that

What's the idiomatic equivalent of C structs in Lisp?

Deadly 提交于 2019-12-31 18:55:02
问题 In C-type languages, there is a strong emphasis on structs/records and objects from the very beginning and in every introductory book. Then, their complete systems are designed around managing such structs, their mutual relations and inheritance. In Lisp documentation, you can usually find 1-2 pages about how Lisp "also" has a defstruct, a simple example, and thats usually it. Also, nesting of structures is never mentioned at all. For someone coming from a C background, it first seems that

Golang - Capitals in struct fields

不问归期 提交于 2019-12-31 17:49:23
问题 I'm using this library to access couchDB (cloudant to be specific) "github.com/mikebell-org/go-couchdb" and I've noticed a problem. When I go to add a file to the database and pass in a struct, only the fields of the struct which started with a capital letter get added. For example type Person struct { name string Age int } func main() { db, _ := couchdb.Database(host, database, username, password) joe := Person{ name: "mike", Age: 190, } m, _ := db.PostDocument(joe) } In this case, only the

Difference between -> and . in a struct?

梦想与她 提交于 2019-12-31 09:00:08
问题 If I have a struct like struct account { int account_number; }; Then what's the difference between doing myAccount.account_number; and myAccount->account_number; or isn't there a difference? If there's no difference, why wouldn't you just use the . notation rather than -> ? -> seems so messy. 回答1: -> is a shorthand for (*x).field , where x is a pointer to a variable of type struct account , and field is a field in the struct, such as account_number . If you have a pointer to a struct, then

Union in Struct Error

六月ゝ 毕业季﹏ 提交于 2019-12-31 07:35:39
问题 I have the following struct: struct type1 { struct type2 *node; union element { struct type3 *e; int val; }; }; When initialising a pointer *f that points to an instance of type1 and doing something like: f.element->e or even just f.element , I get: error: request for member ‘element’ in something not a structure or union What am I overseeing here? 回答1: element is the name of the union, not the name of a member of type1 . You must give union element a name: struct type1 { struct type2 *node;

struct declaration

喜你入骨 提交于 2019-12-31 07:03:28
问题 What is the difference between these 2 ways of declaring a struct? First way: struct x {}; Second way: struct _x {} x; 回答1: The first defines only the type struct x . The second defines the type struct _x and defines a variable of that type named x . Though it's probably not what you had in mind, names starting with an underscore like _x are reserved at file scope, so unless this is inside some other scope, the second has undefined behavior. 回答2: The second way declares a variable named the

Modifying struct members through an interface in Go

烈酒焚心 提交于 2019-12-31 07:00:54
问题 I've come to a point in a Go project of mine where I'd like to create multiple subclasses of a base class, and be able to operate on instances of the subclasses through a base class/interface variable (I'm using the word "class" even though the concept doesn't really exist in Go). Here's what it might look like in C++ just to show what I mean: #include <iostream> using namespace std; class Base { public: int x,y; virtual void DoStuff() {}; }; class Thing : public Base { public: void DoStuff()