struct

Is this usage of the const keyword in line with its intention?

蓝咒 提交于 2019-12-23 04:05:18
问题 I am designing an API and am considering the use of "immutable" structs", or "read-only structs". Using a simplified example, this could look something like: struct vector { const float x; const float y; }; struct vector getCurrentPosition(void); struct vector getCurrentVelocity(void); Everything works fine as long as I return the immutable struct on the stack. However, I run into issues when implementing a function like: void getCurrentPositionAndVelocity( struct vector *position, struct

Emulating Classes in C using Structs

早过忘川 提交于 2019-12-23 03:59:13
问题 I am constrained to using C for a competition and I have a need to emulate classes. I am trying to construct a simple "point" class that can return and set the X and Y coordinates of a point. Yet, the below code returns errors such as "unknown type name point", "expected identifier or (" and "expected parameter declarator." What do these errors mean? How do I correct them? Is this the correct approach to writing a "pseudo-class"? typedef struct object object, *setCoordinates; struct object {

Accessing and changing structs as property vs as field

那年仲夏 提交于 2019-12-23 03:46:13
问题 Ok, I'll start my question saying that I understand the evil behind mutable structs, but I'm working with SFML.net and using a lot of Vector2f and such structs. What I don't get it is why I can have, and change the values of, a field in a class and can't do the same with a property, in the very same class. Take a look at this code: using System; namespace Test { public struct TestStruct { public string Value; } class Program { TestStruct structA; TestStruct structB { get; set; } static void

Value type class definition in C#?

懵懂的女人 提交于 2019-12-23 03:46:08
问题 Is it possible to make a class that is not a struct but is a value type, or is like a value type in that it copies on being passed instead of being passed by reference. edit: Sorry about the question having to be edited after being asked. Also, see this question for more information. Cycle in the struct layout that doesn't exist 回答1: EDIT 2 As it now seems, you're looking to declare a true value type using the class keyword, which is by definition not possible. Since you're looking at

How to send a struct over a pipe C++

时间秒杀一切 提交于 2019-12-23 03:13:50
问题 I am very new to writing in c++ and am working on using pipes to communicate between processes. I have written a very simple program that works when I am sending strings or integers but when I try to send a struct (message in this case) I get null when I try to read it on the other side. Does anyone have some insight into this that they would share? Thanks for your time. #include <unistd.h> #include <iostream> #include <stdlib.h> #include <stdio.h> #include <string.h> #define BUFFER_LEN

If struct A is embedded in B, can methods on A access method and fields of B?

不打扰是莪最后的温柔 提交于 2019-12-23 03:12:31
问题 struct A {} func (a *A) BName(id int) string { return a.Name } struct B { *A Name string } func main() { b := &B{Name: "abc"} fmt.Println(b.Name) } the code failure, I want know how to write code to achieve, A.BName can access B struct attribute Name 回答1: This is not possible. struct A does not know anything about the types it is embedded into. Think about it, A can be embedded into any other struct, so how could you know ahead of time the type of the struct that A is embedded into. If you

p is a pointer to a structure, what do all these code snippets do?

谁说胖子不能爱 提交于 2019-12-23 03:06:17
问题 ++p->i p++->i *p->i *p->i++ (*p->i)++ *p++->i I don't understand these statements above, I wrote a small test program to understand them. #include <stdio.h> struct my_structure { int i; }; void main() { struct my_structure variable = {20}; struct my_structure *p = &variable; printf("NAME: %d\n", ++p->i); printf("NUMBER: %d\n", p++->i); printf("RANK: %d", *p->i++); printf("name: %d\n", *p->i++); printf("number: %d\n", (*p->i)++); printf("rank: %d", *p++->i); } Here's what the output I got

Is there a C preprocessor macro to print out a struct?

左心房为你撑大大i 提交于 2019-12-23 02:51:16
问题 As far as I can tell, there's no way to print out a struct value in C . i.e., this doesn't fly: typedef struct { int a; double b; } stype stype a; a.a=3; a.b=3.4; printf("%z", a); instead you have to say: printf("a: %d\n", a.a); printf("b: %f\n", a.b); This seems like a perfect place where you could use a macro to save a vast amount of typing for arbitrary structs. Is the C preprocessor powerful enough to perform this transformation? 回答1: I think that the simplest solution (and maybe the most

How can I search for a specific struct value? Maybe a better approach?

泄露秘密 提交于 2019-12-23 02:38:32
问题 I'm trying to find a struct I created earlier that has a specific value. Once I found it, I want to set variables on that struct. I don't know how to do this. Is there a better way of doing this? Maybe classes? Or should structs work? For example, my struct: public struct MyTest { public string device; public string status; public string revision; public string number; public string ledmo; } My Test Code: MyTest thisTest=new MyTest(); thisTest.device=blah; thisTest.number=blah2; MyTest

Initializing an variable matrix in a structure C

£可爱£侵袭症+ 提交于 2019-12-23 01:50:25
问题 I am having trouble with initializing a variable matrix in a structure in C. Have been reading a couple of posts(post) but I cant seem to fix it. Don't ask me why but for an assignment I need to initialize a matrix located in a structure. My code for the struct is: typedef struct maze{ int row; int column; char matrix[row][column]; }maze; By calling a different function, after reading a certain file, I need to initialize a matrix by its given parameters, so 2 parameters "row" and "column". My