代码之家  ›  专栏  ›  技术社区  ›  Andreas

如何将指针强制转换为未命名的结构指针?

c++
  •  1
  • Andreas  · 技术社区  · 7 年前

    前几天我有一个结构:

    struct foo_t {
      char a, b, c;
    } *foo = (foo_t*)untyped_memory;
    

    但是有一个命名类型是过分的。但是,它的未命名形式:

    struct {
      char a, b, c;
    } *bar = untyped_memory;
    

    …未编译,因为指针类型不兼容。

    有什么办法让它起作用吗?

    1 回复  |  直到 7 年前
        1
  •  7
  •   George    7 年前

    decltype

    struct {
      char a, b, c;
    } *bar = (decltype(bar))untyped_memory;