English 中文(简体)
我能否在C中扩展一个结构?
原标题:Can I extend a struct in C?
  • 时间:2011-09-10 08:27:30
  •  标签:
  • c
  • struct
typedef struct foo_s {
    int a;
} foo;

typedef struct bar_s {
    foo;
    int b;
} bar;

基本上,我想做的是:

bar b;
b.a;

I know that i could do b.foo_name.a if I had named the foo struct in bar, but Id prefer not to.

这样做的任何方法?

这个问题得到了各种不同的答复,因此,请允许我解释这一需要。 我之所以要这样做,是因为我有一个需要适应我的情况的图书馆,即我必须修改原来的结构。 此外,我需要做的是,在结构开始之前增加1个项目(从一开始算起)。 因为我有一个标的,领导该项目的所有组成部分。 我可以简单地把你提到的内容混为一谈,但鉴于所有参考资料都需要打上可变和高潮的字眼。 该图像的位置。 确实有10亿种类型。

最佳回答

Evidently 这一特征已添加到C11,但Ilas I没有接触到最近省份的C汇编者(>=海湾合作委员会4.6.2)。

typedef struct foo {
  int a;
} foo;

typedef struct bar {
  struct foo;
  int b;
} bar;

int main() {
  bar b;
  b.a = 42;
  b.b = 99;
  return 0;
}
问题回答

不可能在<代码>C上填写。 但是,你可以在<条码>/条码>中变相继承。

typedef struct bar_s {
    foo obj;
    int b;
} bar;

bar b;
b.obj.a = 10;

If you ment

typedef struct foo_s {
    int a;
  } foo;

typedef struct bar_s {
 foo my_foo;
int b;
} bar;

您可以:

bar b; b.my_foo.a = 3;

否则, 由于<条码>sizeof(bar_s)对汇编时间不利,因此无法在C中做到这一点。 它不是一种良好做法,但你可以节省一个<代码>,避免∗ ptr; Pointer within bar_s, and another enum which打上了ptr打字,并按类型投放。

i.e:

typedef enum internalType{
  INTERNAL_TYPE_FOO = 0,
}internalType_t;

typedef struct bar_s {
 internalType_t ptrType;
 void* ptr;
int b;
} bar;

之后:

bar b;  foo f;
b.ptrType = INTERNAL_TYPE_FOO;
b.ptr = &f;

以及守则的其他部分:

 if (b.ptrType == INTERNAL_TYPE_FOO) {
    foo* myFooPtr = (foo *)b.ptr;
 }

It can be easily done via preprocessor:

Create a file named base_foo.h:

int foo;

Then simply include it:

typedef struct foo_s {
    #include "base_foo.h"
} foo;

typedef struct bar_s {
    #include "base_foo.h"
    int b;
} bar;

匿名机构和没有姓名的工会之间存在混淆。 无名称的领域是微软延伸。

struct known {
    struct /* anonymous */ {
        int anonymous;
    };
    int known;
};

。 它不需要任何外地名称。

一个无名的领域是Microsoft的延伸,允许C的继承有限。

struct A {
    int a;
};

struct B {
    struct A: // nameless field
    int b;
};

匿名<代码>truct或union不是无名外地,无名外地并非匿名,至少是C11标准界定的方式。

You can try using inheritance:

struct foo_s
{
    int a;
};

struct bar_s: foo_a
{
    int b;
};

C++工程,不能确定它是否在C工作。

这是没有国旗的最简单方式

#include <stdio.h>

#define foo_s struct { int a; }
typedef foo_s foo;

typedef struct bar_s {
    foo_s; // extends foo_s
    int b;
} bar;

int main(void)
{
    bar b = {
        .a = 1,
        .b = 2,
    };
    foo *f = (foo *)&b;

    printf("a: %d
", f->a);

    return 0;
}
$ gcc inherit.c
$ ./a.out
a: 1




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签