Does anybody see such an array indexing way like "2[array]"? [duplicate] - c++

This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 8 years ago.
Today, I see such a wierd way of indexing the arry.
The code is like:
int array[] = {10, 20, 30};
cout << -2[array];
I've never seen such a strange way of using array. But there is no compilation error.
Can anyone tell me does the ISO document evolve the description for this way of using array?

It works, because expressions of the form x[y] is just sugar for *(x+y), and of course, the addition is commutative, so 2[array] and array[2] get compiled to the same thing.
Don't do it though, because it's unnecessarily confusing.

Related

Testing if an expression compiles [duplicate]

This question already has answers here:
Portable way to check if expression compiles
(2 answers)
How to check at compile time that an expression is illegal?
(2 answers)
Closed 3 days ago.
Following this question where I ask how to test if an expression that tries to use an input iterator for write compiles (disclaimer, it shouldn't!), I would like to know if it's possible to have a generic solution that, given an expression, we are able to check if the expression will compile or not.
Take an easy example, the input iterator in the linked post. If I try to write with an input iterator:
collections::Array arr = collections::Array<int, 5>{1, 2, 3, 4, 5};
auto it_begin = arr.begin();
*it_begin = 7;
that shouldn't compile, because the operator*() returns a const&.
So, what would be the way, of, given an expression, test if it will compiles?
Extra points (well, just take this in consideration if it's possible):
No macros involved
No SFINAE related stuff

What is this Called in C++? [duplicate]

This question already has answers here:
What is a designated initializer in C?
(2 answers)
Closed last month.
I've seen a piece of code that goes like this:
int charP[] = {
['A'] = 1,
['B'] = 2,
['C'] = 3,
['D'] = 4};
My syntax highlighter for C++ does not recognize it, but the syntax highlighter for C does. It sort of behaves like a Python dictionary, so charP['A'] would spit out 1. Maybe I'm not looking hard enough, but can anyone point me in the right direction because every time I search "C++ dictionary" or "C dictionary", it always involves Maps and not this. What is it? What are its limitations?
In C it's array initialization using a designated initializer. It's covered in section 6.7.9 of the C 2017 specification.
In C++ it's invalid syntax (#Ranoiaetep, #CaptainObvlious).

A strange output in C++ [duplicate]

This question already has answers here:
Strange numbers when array is not initialized in C++ [duplicate]
(3 answers)
What happens to a declared, uninitialized variable in C? Does it have a value?
(9 answers)
Closed 1 year ago.
I have tried a simple code and found a strange error(wrt me)
it is something like s[10]
now i have put some number of values into this array.t
like s[0]=0;s[1]=1.s[2]=2 and all others are empty.
now i put a for loop to see how it goes and to my surprise after index 2, some random numbers popping up in output and idk why. It should be null and the loop should have exited but here, it gives me some output like 01248766575...
why is it happening? pls do help me if u know

Mysterious C++ declaration [duplicate]

This question already has answers here:
How do I use arrays in C++?
(5 answers)
Closed 4 years ago.
Sorry for the dumb question (I'm a total C++ noob), but what does
int x[101010];
declare? Is it list, a vector et cetera? And what is the meaning of 101010? I have never seen a declaration like this.
At block scope, int x[101010]; declares a uninitialised array with 101010 elements.
At global scope, the effect is similar but the elements are set to 0.
Note that if you had written int x[010101];, then you would have created 4161 elements as a leading 0 denotes an octal literal in C++.
In C++, a good rule of thumb is to use a std::vector unless you have a good reason not to.

Comparing chars together? [duplicate]

This question already has answers here:
How do I properly compare strings in C?
(10 answers)
Closed 6 years ago.
I'm fairly new to C++ and have run into a problem when trying to compare two chars, here is an example:
#define PartOne "He"
#define PartTwo "llo"
char Final1Var[] = PartOne PartTwo;
char ComapreVars[] = "Hello";
if(Final1Var == ComapreVars)//This is were the problem occurs, the chars are supposed to be equal to each other BUT for some reason the 'if' statement ends up determining they're not?
InGameDialog::Alert("They Match");
else
InGameDialog::Alert("They Don't Match");
What is going wrong with the code? I can't imagine why this wouldn't work? Any suggestions?
in c++ character array's could not be compared using == operator you have to use strcmp function or string comparison function.
This is a very common question. I'll mark it as duplicate once I find a good answer to this among the other questions.
In the mean time, you're not comparing chars, you're comparing char[]s, which is entirely different. You'll want to use strcmp, strncmp, or std::string types would be an even better solution.
What is array decaying? has some reasonable explanations for what's going on in your code and why.