结构指针运算符猜想(理论) [英] Structure pointer operator conjecture (theory)
问题描述
指向结构的指针使用,所以往往是有它特殊的运算符: - >。下面前pressions是等价的:
(* X).Y
X-GT&; Y
难道是公平的认为仅仅作为preprocessor宏定义为这样该运营商:
的#define(X) - GT; (*(x)的。)
为什么或为什么不?抑或是codeD,因为从一开始运营商 - ?怎么会变成这样不同的/有利
只是好奇。
下面前pressions是等价的:
块引用>(* X).Y
X-GT&; Y是,两者都访问结构成员两种不同的方式
是
。
(* X).Y
操作符是。
DOT与价值变量的作品元素选择参照
。其原因* 使用。手段
X
是指向struct。
X-> Y
运营商- >
用于名为元素选择通过指针
。这与指针的工作结构体。这是*
不使用这一次的原因。这两部作品一样。
难道是公平的认为这个操作者只需为preprocessor宏定义为这样的:
块引用>的#define(X) - GT; (*(X)。)
否首先,它给出的错误: 宏名必须是标识符的。这个错误是因为我们不能有
- >
运营商的宏名。在<一个href=\"http://stackoverflow.com/questions/369495/what-are-the-valid-characters-for-macro-names?answertab=votes#tab-top\">valid宏名的可以是:
宏名称应只由字母数字字符和下划线,即A-Z','A-Z','0-9'和'_',且第一个字符不应该是一个数字。有些preprocessors还允许美元符号$,但你不应该使用它;不幸的是我无法引用C标准,因为我没有它的一个副本。
块引用>另外,还要注意
- &GT;
和区别运营商为我上述状态。也是他们的precedence不同所以它的坏主意,与其他替换一个运营商。
有效宏来访问结构元素:
块引用>此外,我想今天分享只有我才知道,大多数的C头文件。定义的宏这样的:
的#define S(X)((X).Y)
有关具体strcut元素。
围绕是覆盖
的通知
(X)
括号X的precedence *
在。
。默认情况下。
DOT拥有超过高出precedence *
因此,它可以使用了指针和简单变量。下面我举的例子会有所帮助,我认为。的#define S(X)((X).Y)
typedef结构{
诠释Ÿ;
}X;
X * X;
X b的;
诠释主(){
S(* X);
S(B);
}编辑:结果
更好的选择。我伸出我的想法来访问strcut元素,我定义了新的宏:
的#define S(X,Y)((X).Y)
typedef结构{
int类型的;
}X;
X * X;
X b的;
诠释主(){
S(* X,A);
S(B,A);
}不是它不为特异性通过宏元素。
在希望 - 至少OP喜欢它:)
Pointers to structures are used so often that there is a special operator for it: ->. The below expressions are equivalent:
(*x).y x->y
Would it be fair to think of this operator simply as preprocessor macro defined as such:
#define (x)-> (*(x).)
Why or why not? Or was it coded as a operator from the start - and how would this be different / advantageous?
Just curious.
解决方案The below expressions are equivalent:?
(*x).y x->y
Yes, Both are two different ways to access structure member
y
.
(*x).y
operator is.
DOT that works with value variableElement selection by reference
. that the reason*
used. meansx
is pointer to struct.
x->y
operator->
is used calledElement selection through pointer
. This work with pointer to struct. that is the reason*
not used this time.Both works same.
Would it be fair to think of this operator simply as preprocessor macro defined as such:
#define (x)-> (*(x).)
No First it give an error: macro names must be identifiers. This error is because we can't have
->
operator as macro name.a valid macro name can be:
Macro names should only consist of alphanumeric characters and underscores, i.e. 'a-z', 'A-Z', '0-9', and '_', and the first character should not be a digit. Some preprocessors also permit the dollar sign character '$', but you shouldn't use it; unfortunately I can't quote the C standard since I don't have a copy of it.
Also, note
->
and.
are differences operators as I state above. also their precedence are different so its bad idea to replace one operator with other.Valid macros to access struct elements:
Additionally I would like to share today only i came to know that most C header files. Defined macros like:
#define S(x) ((x).y)
for specific strcut element.
Notice
(x)
parenthesis aroundx
is to overwrite precedence of*
over.
. By default.
DOT has higher precedence over*
So that it can be use for pointer and simple variable. Below my example will be helpful I think.#define S(x) ((x).y) typedef struct { int y; }X; X *x; X b; int main(){ S(*x); S(b); }
EDIT:
Better OptionI am extending my idea to access strcut elements, I defined new macro:
#define S(x,y) ((x).y) typedef struct { int a; }X; X *x; X b; int main(){ S(*x,a); S(b,a); }
Not its not more for specif elements via macros.
Hope at-least OP love it :)
这篇关于结构指针运算符猜想(理论)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!