struct

No == operator found while comparing structs in C++

两盒软妹~` 提交于 2019-12-28 08:03:39
问题 Comparing two instances of the following struct, I receive an error: struct MyStruct1 { MyStruct1(const MyStruct2 &_my_struct_2, const int _an_int = -1) : my_struct_2(_my_struct_2), an_int(_an_int) {} std::string toString() const; MyStruct2 my_struct_2; int an_int; }; The error is: error C2678: binary '==' : no operator found which takes a left-hand operand of type 'myproj::MyStruct1' (or there is no acceptable conversion) Why? 回答1: In C++, struct s do not have a comparison operator generated

How to clone a structure with unexported field?

老子叫甜甜 提交于 2019-12-28 06:48:25
问题 If I have a type defined as: type T struct { S string is []int } then how can I go about cloning an object of this type? If I do a simple assignment: p := T{"some string", []int{10, 20}} q := p Then any changes made to the []int affect both objects. Since T.is is not exported, it cannot be copied over explicitly, even if extracted using reflect. I'm currently supplying a Clone method in the package of the type itself. But that doesn't help with similar types in other packages. Is there

Memory layout of struct having bitfields

不羁的心 提交于 2019-12-28 06:33:21
问题 I have this C struct: (representing an IP datagram) struct ip_dgram { unsigned int ver : 4; unsigned int hlen : 4; unsigned int stype : 8; unsigned int tlen : 16; unsigned int fid : 16; unsigned int flags : 3; unsigned int foff : 13; unsigned int ttl : 8; unsigned int pcol : 8; unsigned int chksm : 16; unsigned int src : 32; unsigned int des : 32; unsigned char opt[40]; }; I'm assigning values to it, and then printing its memory layout in 16-bit words like this: //prints 16 bits at a time

Nested structures

旧街凉风 提交于 2019-12-28 05:53:07
问题 The following code compiles on a C++ compiler. #include<cstdio> int main() { struct xx { int x; struct yy { char s; struct xx *p; }; struct yy *q; }; Would there be any difference in behavior while compiling with a C compiler? i.e. would there be any compiler error? 回答1: The code in your post is obviously incomplete, just declarations, so it is hard to say anything conclusive. On obvious difference is that in C++ the inner struct type will be a member of outer struct type, while in C language

undefined C struct forward declaration

元气小坏坏 提交于 2019-12-28 05:43:08
问题 I have a header file port.h, port.c, and my main.c I get the following error: 'ports' uses undefined struct 'port_t' I thought as I have declared the struct in my .h file and having the actual structure in the .c file was ok. I need to have the forward declaration as I want to hide some data in my port.c file. In my port.h I have the following: /* port.h */ struct port_t; port.c: /* port.c */ #include "port.h" struct port_t { unsigned int port_id; char name; }; main.c: /* main.c */ #include

What does this error mean: “error: expected specifier-qualifier-list before 'type_name'”?

偶尔善良 提交于 2019-12-28 05:35:09
问题 I've been working on the Cell processor and I'm trying to create a struct that will hold an spe_context_ptr_t , which will be used within the thread to launch an spe context and will also hold a pointer to something else that will be passed to the spu context from within the thread (currently I'm trying to just make it a generic pointer, but in actuality it will be a pointer to another structure I've defined). When I try and compile, I get the following error: spu/../common.h:38: error:

Swift: Pass Uninitialized C Structure to Imported C function

时间秒杀一切 提交于 2019-12-28 04:23:30
问题 I'm aware of this answer, but this is not the same thing - thats passing a pointer to be initialised with an allocation. I'm interfacing with a C library that has the following structure definition: typedef struct myStruct { unsigned char var [50]; } myStruct; There is a function to which you pass the address of the structure - normally stack based not heap, thus: myStruct mine; initMyStruct(&mine); This initialises the content of the struct - the caller does not know the internal format of

Typedefs, tagged and untagged structures, and incompatible pointer types

二次信任 提交于 2019-12-28 04:14:49
问题 Consider the following C code snippet: typedef struct node{ int val; struct node* left; struct node* right; }node; void inorderTraversal(node *p){ inorderTraversal(p->left); printf("%d",p->val); inorderTraversal(p->right); } If I write only typedef struct instead of typedef struct node , I get a warning saying "passing argument of incompatible pointer type" when I call inorderTraversal(root) in main() . Why do I get this warning even though the program doesn't show any error? 回答1: If you don

Efficient Go serialization of struct to disk

放肆的年华 提交于 2019-12-28 04:10:10
问题 I've been tasked to replace C++ code to Go and I'm quite new to the Go APIs. I am using gob for encoding hundreds of key/value entries to disk pages but the gob encoding has too much bloat that's not needed. package main import ( "bytes" "encoding/gob" "fmt" ) type Entry struct { Key string Val string } func main() { var buf bytes.Buffer enc := gob.NewEncoder(&buf) e := Entry { "k1", "v1" } enc.Encode(e) fmt.Println(buf.Bytes()) } This produces a lot of bloat that I don't need: [35 255 129 3

Are C# structs thread safe?

大憨熊 提交于 2019-12-28 04:09:07
问题 Is a C# struct thread-safe? For example if there is a: struct Data { int _number; public int Number { get { return _number; } set { _number = value; } } public Data(int number) { _number = number; } } in another type: class DadData { public Data TheData { get; set; } } is property named TheData, thread-safe? 回答1: No, structures in .NET are not intrinsically thread-safe. However, the copy-by-value semantics that structures have great relevance to this converation. If you are passing your