This question already has answers here:
How do I use arrays in C++?
(5 answers)
Closed 3 years ago.
I wanted to assign a boolean value to a two-dimensional boolean array, but the compiler showed an error
bool Amass[100][80];
Amass[1,1] = true; //even so I see only an error
You have declared a two-dimensional array
bool Amass[100][80];
However in this statement
Amass[1,1] = true;
in the subscript operator expression you are using the comma operator. Its result is the right-most operand. That is the statement is equivalent to
Amass[1] = true;
So in the left side of the assignment there is used a one-dimensional array.
It seems you mean
Amass[1][1] = true;
Related
This question already has answers here:
Is short-circuiting logical operators mandated? And evaluation order?
(7 answers)
Closed 2 years ago.
As the title says: Is there any guarantee from C++ Standard to be sure the left side of && (or and) operator always evaluated first? To be honest, I couldn't search in C++17 Standard, I don't know which section I most look for.
Sample of Problem:
I want to do something like this:
std::unordered_map<std::size_t, std::weak_ptr<T>> objects;
bool f (std::size_t const id) {
bool result = false;
if (not objects.at(id).expired()) {
auto const& object = objects.at(id).lock();
/* Here left side of `and` most be evaluated first */
result = object->parent->remove(id) and
objects.erase(id) == 1;
}
return result;
}
And want to be sure there is no problem with the code.
[expr.log.and]/1 ... Unlike &, && guarantees left-to-right
evaluation: the second operand is not evaluated if the first operand is false.
This question already has answers here:
C++, ternary operator and cout
(2 answers)
Conditional operator used in cout statement
(3 answers)
Closed 2 years ago.
When I write
std::cout << 5 ? 'a' : 'b';
the output is 5? Why should I put the whole expression in parentheses? isn't the ternary operator as a whole an expression and should be evaluated first then output the result? And if std::cout consider that I wanna print the condition, what happens to the part after the condition? Why is it ignored?
This question already has answers here:
Why does i[arr] work as well as arr[i] in C with larger data types?
(5 answers)
Closed 7 years ago.
The following code
#include<stdio.h>
int main()
{
int arr[] = {10,20,30};
cout << -2[arr];
return 0;
}
prints -30. How? Why?
In your case,
cout<<-2[arr];
gets translated as
cout<<-(arr[2]);
because,
array indexing boils down to pointer arithmatic, so, the position of array name and the index value can be interchanged in notation.
The linked answer is in C, but valid for C++ also.
regarding the explicit ()s, you can check about the operator precedence here.
Look at this statement
cout << -2[arr];
First, know that even though it looks strange the following is true
2[arr] == arr[2]
That being said operator[] has higher precedence than -. So you are actually trying to invoke
-(arr[2])
In C and C++, 2[arr] is actually the same thing as arr[2].
Due to operator precedence, -2[arr] as parsed as -(2[arr]). This means that the entire expression evaluates to negating the 3rd element of arr, i.e. -30.
This question already has answers here:
sizeof an array passed as function argument [duplicate]
(3 answers)
Why do C and C++ compilers allow array lengths in function signatures when they're never enforced?
(10 answers)
Closed 8 years ago.
int size(int arr1[])
{
int size1=sizeof(arr1)/sizeof(int);
cout<<size1<<endl;
return size1;
}
void main()
{
int b[]={1,2,3,4,5};
int size2 = size(b);
cout<<size2<<endl;
for (int i=0;i<size2;i++)
{
cout<<b[i];
}
}
I have put the b[] function into size() and check the size then return value.
however, it just return 1 as the answer.
Can anyone please help me to solve this.
A beginner of C++
sizeof(arr1) in the function returns the size of a pointer, not of the whole array.
That´s just how the language is.
You´ve to determine the array size without sizeof:
Either pass a second parameter with the number, or fill the array in a way
you can find the end because a certain value is there (and nowhere else)
This question already has answers here:
How does the Comma Operator work
(9 answers)
Expression "variable, variable = value;"
(4 answers)
Closed 9 years ago.
So, I'm doing homework. I've encountered something I haven't seen before and cannot find a decent explanation of what it does. Basically,
Object object;
...
while((value1, value2) = function(object)) {
object.foo(value1, value2);
}
The (value1, value2) in the while statement really throws me. Any ideas?
Its a comma operator.
The result of the comma operator is the last value (the others are evaluated and discarded).
while((value1, value2) = function(object)) {
object.foo(value1, value2);
}
If value1 is just a variable and not an expression then it is equivalent too:
while(value2 = function(object)) {
object.foo(value1, value2);
}
If value1 is an expression then it is evaluated each time around the loop. The result is discarded, but if the expression has side effects these will take effect.