标签: struct

struct中的匿名联合不在c99中?

这里是我所遇到的非常简化的问题代码:

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’ …

c gcc struct c99 unions

49
推荐指数
5
解决办法
3万
查看次数

48
推荐指数
3
解决办法
3万
查看次数

使用Python类作为数据容器

有时将相关数据聚集在一起是有意义的.我倾向于用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,因为我喜欢最小化代码行数.我的同事认为如果使用类,代码更具可读性,并且将来更容易向类中添加方法.

你更喜欢哪个?为什么?

python struct dictionary class

48
推荐指数
8
解决办法
5万
查看次数

如何根据定义使'struct'为Nullable?

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# struct nullable

48
推荐指数
3
解决办法
5万
查看次数

使用==比较两个结构

我试图在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 equality

48
推荐指数
3
解决办法
5万
查看次数

如何在C++类的初始化列表中初始化member-struct?

我在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)在初始化列表中的作用是什么?

或者结构甚至从编译器自动初始化为零?

c++ constructor struct list initializer

47
推荐指数
2
解决办法
7万
查看次数

C - 释放结构

假设我有这个结构

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 malloc struct

47
推荐指数
4
解决办法
8万
查看次数

C空结构 - 这是什么意思/做什么?

我在一个头文件中找到了我需要使用的设备的代码,虽然我已经做了多年的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 struct

47
推荐指数
2
解决办法
1万
查看次数

在C中创建"类",在堆栈上与堆相比?

每当我看到一个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中似乎是可以接受的.为什么堆分配的结构"类"如此常见?

c heap stack struct

47
推荐指数
4
解决办法
5102
查看次数

Golang:不同结构类型之间的转换是否可能?

假设我有两种类似的类型:

type type1 []struct {
    Field1 string
    Field2 int
}
type type2 []struct {
    Field1 string
    Field2 int
}
Run Code Online (Sandbox Code Playgroud)

是否有直接的方法将值从type1写入type2,知道它们具有相同的字段?(除了编写一个循环,将所有字段从源复制到目标)

谢谢.

struct go

46
推荐指数
7
解决办法
6万
查看次数

标签 统计

struct ×10

c ×4

c# ×3

class ×2

.net ×1

c++ ×1

c99 ×1

constructor ×1

dictionary ×1

equality ×1

gcc ×1

go ×1

heap ×1

initializer ×1

list ×1

malloc ×1

nullable ×1

python ×1

stack ×1

types ×1

unions ×1