This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 2 years ago.
I've seen an example showing as
int n = sizeof(0)["abcdefghij"];
cout<<n;
What does that thing in square brackets mean? I've read somewhere that (0)["abc"] is equivalent to ("abc")[0]. Meaning the above expression is simply
n = sizeof("abcdefghij")[0];
i.e. the first element.
First, sizeof is not a function but an operator
sizeof(0)["abcdefghij"] can be parsed as either
sizeof( (0)["abcdefghij"] ), or
( sizeof(0) )["abcdefghij"]
Since sizeof has lower precedence than [], the former will take place
(0)["abcdefghij"] is equivalent to "abcdefghij"[0] which is just 'a', so the whole thing is the same as sizeof('a') which is 1 in C++
Demo on GodBolt, ideone
If you replace sizeof(0) with sizeof(int) then the same thing happens, but now (int)["abcdefghij"] is invalid so it should result in a compilation fail. Most compilers report an error as expected that except ICC so it looks like that's an ICC bug which chooses (sizeof(int))["abcdefghij"] over sizeof((int)["abcdefghij"]) just because the latter is invalid
Related: Why does sizeof(my_arr)[0] compile and equal sizeof(my_arr[0])?
Related
This question already has answers here:
Why does the most negative int value cause an error about ambiguous function overloads?
(3 answers)
Closed 3 years ago.
I'm trying to write a test case for some corner case. For input of type int64_t, the following line won't compile:
int64_t a = -9223372036854775808LL;
The error/warning is:
error: integer constant is so large that it is unsigned [-Werror]
I thought the number was out of range, so I tried:
std::cout << std::numeric_limits<int64_t>::min() << std::endl;
It outputs exactly the same number!!! So the constant is within the range.
How can I fix this error?
You may write
int64_t a = -1 - 9223372036854775807LL;
The problem is that the - is not part of the literal, it is unary minus. So the compiler first sees 9223372036854775808LL (out of range for signed int64_t) and then finds the negative of this.
By applying binary minus, we can use two literals which are each in range.
Ben's already explained the reason, here's two other possible solutions.
Try this
int64_t a = INT64_MIN;
or this
int64_t a = std::numeric_limits<int64_t>::min();
This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 5 years ago.
I came across this code I have no clue what it is doing
// ptr is a pointer to an int
if (2[ptr] == 5){
// do something
}
I did some of my tests but no matter what value I pass into ptr, 0, negatives, positives, 2[ptr] always evaluates to 0. I am surprised it actually compiles. Can someone explain what this expression is ?
2[ptr] is the same as ptr[2] (more or less). Somebody is playing a little joke on you.
As for why it always evaluates to 0, that depends on the rest of the program, which we cannot see.
i have a question on pointer concept which i could not find a logical answer to
#include<conio.h>
#include<iostream.h>
void main()
{
int arr[10];
clrscr();
cout<<*arr+5 - *arr+3;
getch();
}
even if i assign arr[0]=10; (or any other value)
the compiler gives answer 8 but how . I can not see(understand) how operator precedence and associativity does solve it.
I will be grateful to you.
Because of *arr - *arr is 0 and 5 + 3 is 8.
The result you may be expecting is the result of:
cout<<(*arr+5) - (*arr+3);
The compiler gives answer 8 because that operation is simply equivalent to: (*arr - *arr) + 5 + 3 = 8. If you want to add the scalar to the pointer and then get the referenced value, you have to use parentheses *(arr+5).
If you look at the precedence table, for example here:
http://en.cppreference.com/w/cpp/language/operator_precedence
then you'll notice that the dereference operator (*) has higher priority than addition/subtraction (+/-) operators (they are in group no. 3 and 6 respectively). This is why the first operation that is performed is getting the value that the arr variable is pointing to, i.e. this part:
*arr
After this, the addition/subtraction is performed. The value that arr is pointing to doesn't matter since it gets reducted anyway.
This is how you should read this expression:
(*arr) + 5 - (*arr) + 3
and (*arr) - (*arr) is 0, no matter what value it points to.
EDIT: What I've written above is apparently true in your case and your compiler, but look at the #Konrad Rudolph comments to this answer.
And, if you are curious, how the compiler knows if, for example, the '*' should be treated as multiplication or dereference operator: it resolves this problem by looking at the number of arguments - if there's only one, than it's derefence, and if there are two, then it's multiplying.
This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 9 years ago.
What will be the output of code
int a=3,b=4;
++a*=++b;
cout<<a<<endl;
compiler shows a=20 output.How precedence and operator associativity is being used here?
What I understand is:
first b on the left of ++a*=++b; is incremented us its unary operator then comes the
turn of *= so
expression becomes ++a = a * ++b; as a=3 and b=5 now so it becomes 15 then 15 is assigned to a and incremented.Finally getting 16 but compiler gives 20
In your particular case on your particular compiler, it seems that first a is incremented to 4 and b is incremented to 5, then a *= b executes and a becomes 20 (4*5). However other compiler could give different result because it is not a defined behaviour as people mentioned in comments
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In C arrays why is this true? a[5] == 5[a]
How is it possible that this is valid C++?
void main()
{
int x = 1["WTF?"];
}
On VC++10 this compiles and in debug mode the value of x is 84 after the statement.
What's going on?
Array subscript operator is commutative. It's equivalent to int x = "WTF?"[1]; Here, "WTF?" is an array of 5 chars (it includes null terminator), and [1] gives us the second char, which is 'T' - implicitly converted to int it gives value 84.
Offtopic: The code snippet isn't valid C++, actually - main must return int.
You can read more in-depth discussion here: In C arrays why is this true? a[5] == 5[a]
int x = 1["WTF?"];
equals to
int x = "WTF?"[1];
84 is "T" ascii code
The reason why this works is that when the built-in operator [] is applied to a pointer and an int, a[b] is equivalent to *(a+b). Which (addition being commutative) is equivalent to *(b+a), which, by definition of [], is equivalent to b[a].