这里是我所遇到的非常简化的问题代码:
enum node_type {
t_int, t_double
};
struct int_node {
int value;
};
struct double_node {
double value;
};
struct node {
enum node_type type;
union {
struct int_node int_n;
struct double_node double_n;
};
};
int main(void) {
struct int_node i;
i.value = 10;
struct node n;
n.type = t_int;
n.int_n = i;
return 0;
}
我不明白的是:
$ cc us.c $ cc -std=c99 us.c us.c:18:4: warning: declaration does not declare anything us.c: In function ‘main’: us.c:26:4: error: ‘struct node’ …
有时将相关数据聚集在一起是有意义的.我倾向于用dict这样做,例如,
self.group = dict(a=1, b=2, c=3)
print self.group['a']
Run Code Online (Sandbox Code Playgroud)
我的一位同事更喜欢创建一个班级
class groupClass(object):
def __init__(a, b, c):
self.a = a
self.b = b
self.c = c
self.group = groupClass(1, 2, 3)
print self.group.a
Run Code Online (Sandbox Code Playgroud)
请注意,我们没有定义任何类方法.
我喜欢使用dict,因为我喜欢最小化代码行数.我的同事认为如果使用类,代码更具可读性,并且将来更容易向类中添加方法.
你更喜欢哪个?为什么?
struct AccountInfo
{
String Username;
String Password;
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我想要一个Nullable实例,我应该写:
Nullable<AccountInfo> myAccount = null;
Run Code Online (Sandbox Code Playgroud)
但我希望struct Nullable自然而然地可以像这样使用(不使用Nullable<T>):
AccountInfo myAccount = null;
Run Code Online (Sandbox Code Playgroud) 我试图在C#中使用equals(==)比较两个结构.我的结构如下:
public struct CisSettings : IEquatable<CisSettings>
{
public int Gain { get; private set; }
public int Offset { get; private set; }
public int Bright { get; private set; }
public int Contrast { get; private set; }
public CisSettings(int gain, int offset, int bright, int contrast) : this()
{
Gain = gain;
Offset = offset;
Bright = bright;
Contrast = contrast;
}
public bool Equals(CisSettings other)
{
return Equals(other, this);
}
public override bool Equals(object obj)
{
if …Run Code Online (Sandbox Code Playgroud) 我在c ++中有以下类定义:
struct Foo {
int x;
char array[24];
short* y;
};
class Bar {
Bar();
int x;
Foo foo;
};
Run Code Online (Sandbox Code Playgroud)
并且想要在Bar类的初始值设定项中将"foo"结构(及其所有成员)初始化为零.可以这样做:
Bar::Bar()
: foo(),
x(8) {
}
Run Code Online (Sandbox Code Playgroud)
......?
或者foo(x)在初始化列表中的作用是什么?
或者结构甚至从编译器自动初始化为零?
假设我有这个结构
typedef struct person{
char firstName[100], surName[51]
} PERSON;
Run Code Online (Sandbox Code Playgroud)
我正在通过malloc分配空间并用一些值填充它
PERSON *testPerson = (PERSON*) malloc(sizeof(PERSON));
strcpy(testPerson->firstName, "Jack");
strcpy(testPerson->surName, "Daniels");
Run Code Online (Sandbox Code Playgroud)
释放该结构所占用的所有内存的正确和安全的方法是什么?是"免费的(testPerson);" 够了还是我需要逐个释放每个结构的属性?
它引出了另一个问题 - 结构如何存储在内存中?我注意到一种奇怪的行为 - 当我尝试打印结构地址时,它等于它的第一个属性的地址.
printf("Structure address %d == firstName address %d", testPerson, testPerson->firstName);
Run Code Online (Sandbox Code Playgroud)
这意味着这个free(testPerson)应该等于这个free(testPerson-> firstName);
那不是我想做的.
谢谢
我在一个头文件中找到了我需要使用的设备的代码,虽然我已经做了多年的C,但我从来没有遇到过:
struct device {
};
struct spi_device {
struct device dev;
};
Run Code Online (Sandbox Code Playgroud)
它用作:
int spi_write_then_read(struct spi_device *spi,
const unsigned char *txbuf, unsigned n_tx,
unsigned char *rxbuf, unsigned n_rx);
Run Code Online (Sandbox Code Playgroud)
还在这里:
struct spi_device *spi = phy->spi;
Run Code Online (Sandbox Code Playgroud)
在哪里定义相同.
我不确定这个定义的重点是什么.它位于主板的Linux应用程序的头文件中,但它对它的使用感到困惑.任何解释,想法?之前见过这个的人(我相信你们有些人:).
谢谢!:BP:
每当我看到一个C"类"(通过访问将指针作为第一个参数的函数来使用的任何结构)时,我看到它们实现如下:
typedef struct
{
int member_a;
float member_b;
} CClass;
CClass* CClass_create();
void CClass_destroy(CClass *self);
void CClass_someFunction(CClass *self, ...);
...
Run Code Online (Sandbox Code Playgroud)
在这种情况下,它CClass_create始终malloc是内存并返回指向它的指针.
每当我看到newC++不必要地出现时,它似乎通常会让C++程序员疯狂,但这种做法在C中似乎是可以接受的.为什么堆分配的结构"类"如此常见?
假设我有两种类似的类型:
type type1 []struct {
Field1 string
Field2 int
}
type type2 []struct {
Field1 string
Field2 int
}
Run Code Online (Sandbox Code Playgroud)
是否有直接的方法将值从type1写入type2,知道它们具有相同的字段?(除了编写一个循环,将所有字段从源复制到目标)
谢谢.