Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subscripting a string literal

Tags:

c++

Is subscripting an alphanumeric a common/valid technique? And what are the implicit conversions that take place ? example :

#include <iostream>
using namespace std;

int main() 
{
    int k(2);
    cout << "Hello"[k] << endl;
    cout << (k-1)["Hello"] << endl;
    // your code goes here
    return 0;
}
like image 560
Nikos Athanasiou Avatar asked Dec 19 '25 04:12

Nikos Athanasiou


1 Answers

It's not particular common to index literal strings, but it has its uses, e.g.

auto hex_digit( int const value )
    -> char
{
    assert( 0 < value && value < 0x10 );
    return "0123456789ABCDEF"[value];
}
like image 64
Cheers and hth. - Alf Avatar answered Dec 20 '25 22:12

Cheers and hth. - Alf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!