简体   繁体   English

在C中重新定义

[英]Redefinition in C

can somebody explain me about redefinition in C: 有人可以向我解释有关C语言中的重新定义的问题:

is it possible to do something like this 有可能做这样的事情吗

typedef struct NumberContainer* ptrNumberContainer;

and after that 在那之后

  typedef struct NumberContainer* ptrCall;

may it cause some problems during linkage? 链接期间可能会引起一些问题吗? thanks in advance 提前致谢

No, that's perfectly OK - you have two synonyms for the same underlying type - this is quite common. 不,那完全没问题-您对于同一个基础类型有两个同义词-这很常见。 However, the practice of hiding the fact that something is a pointer by using a typedef is generally viewed as bad practice in C. 但是,在C语言中,通常认为通过使用typedef隐藏某物是指针这一事实是一种不好的做法。

Of course it's possible. 当然可以。 Defines two different type names to mean the same thing. 定义两个不同的类型名称来表示同一件事。

In that case, ptrCall will actually refer to the same type as ptrNumberContainer . 在这种情况下, ptrCall实际上将引用与ptrNumberContainer相同的类型。 I think they will also be compatible compile time. 我认为它们也会兼容编译时间。 So you can say, for example: 因此,您可以说,例如:

ptrNumberContainer p1;
ptrCall p2;

Then these will work: 然后这些将起作用:

p1 = p2;
*p1 = *p2;

This is not redefinition. 这不是重新定义。 Redefinition refers to macro definitions: 重新定义是指宏定义:

#define FOOBAR 1
#undef FOOBAR
#define FOOBAR 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM