search

Home  >  Q&A  >  body text

c++ - 请问这是什么语法2["abc"]?

cout<<2["abc"]<<endl;
输出c。
不过这是什么语法啊,c++/c的,以前没遇到过,百度无果,有没有老司机带带啊。

高洛峰高洛峰2807 days ago558

reply all(4)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-17 15:06:46

    a[b] is actually *(a + b)
    so the above code is equivalent to

    #include <iostream>
    
    using namespace std;
    
    int main(void) {
        cout << *("abc"+2) << endl;
        return 0;
    }

    Move a stackoverflow: http://stackoverflow.com/ques...

    reply
    0
  • 迷茫

    迷茫2017-04-17 15:06:46

    This example is mentioned on page 144 of "C and Pointers", so I will just screenshot it for you. The author does not recommend writing it this way.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 15:06:46

    Please ask the question under what circumstances did you encounter this problem? I'm a little curious about what this is.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 15:06:46

    Is this a mapping table?

    reply
    0
  • Cancelreply