English 中文(简体)
B. 明显接触特征的模糊方式
原标题:Obfuscated way of accessing a character in string

我今天看到了有趣的法典:

auto ch = (double(), (float(), int()))["	a
"]["abcdefghij"];

其工作方式如下:

char str[] = "abcdefghij";
char ch = str[ 	 ];

为什么甚至有可能? 尤其是,汇编者为什么会把它作为子稿加以描述和使用,而不是 error错?

最佳回答

我解释说:

auto ch = (double(), (float(), int()))["	a
"]["abcdefghij"];

相当于(对所有<条码>代号、浮标、t中与日志操作员的节奏进行合理评价)

auto ch = (0["	a
"])["abcdefghij"];

现在的标准是:

x[y] == *(x + y)

无论谁是协调人。 您:

0["	a
"] == "	a
"[0] ==  	 ;
问题回答

首先,所有<条码>杜布尔和<条码>float 的字眼纯属误导。 com经营者的回报价值是其右侧论点,例如 (double()、 (float()、 int())> boils下限为int(),尽管它以这种方式生成和抛弃了double<>/code>和。 因此考虑:

 auto ch = int()["	a
"]["abcdefghij"];

第一部分将评估。

 int()["	a
"]

现在,认识到<代码>int(>> 缺损------------------------------------------------------------------------------------- 因此,声明相当于:

 0["	a
"]

在C和C++中,“代码>a[b]和b[a]的相当广为人知的切trick是等值,因为该子规范操作者被定义为<代码>a[b] ==*(a + b),而添加是互换的。 因此,这实际上与:

 "	a
"[0]

当然,这等于<条码>。 如今,整个法典是:

 auto ch =  	 ["abcdefghij"];

出于同样原因,这相当于:

 auto ch = "abcdefghij"[ 	 ];

哪一个当然也可以是书面的

char str[] = "abcdefghij";
char ch = str[ 	 ];

如果你给“abcdefghij”以一个名字,并用C++0xauto。 宣布<代码>ch的关键词。

最后,请注意,等于9,因为表象具有ASCII值9,因此,str[ ]str[9].由10个特性组成,随后由NCL 特性终止者()组成,在说明字面上添加,该词句的起首部分为原文。

因此,在这两种情况下,<代码>ch的最终价值为 j





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

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->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签