Why --(-val) is not working? [duplicate] - c++

This question already has an answer here:
Feeling confused with -(--a) vs --(-a) in c
(1 answer)
Closed 7 years ago.
int val = -10;
cout << ++(-val) << endl;
Output should be 11. But it gives me an error.

The error is "lvalue required as increment operand". This means that it's just a value; it does not represent a particular object like "val" anymore so trying to change it makes no sense to the compiler.

Related

looks like there is something wrong with the pre-increment operator in c++ [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Undefined behavior and sequence points
(5 answers)
Closed 19 days ago.
I was just messing with a piece of cpp code that I came across something I couldn't find an explanation for.
when I run this in c++:
int a = 5;
cout << (--a) * (--a);
9 is printed in the console. but when you get the variable (a) from the input, and then give it the same value (5), the result is different:
int a;
cin >> a;
cout << (--a) * (--a);
when you enter 5 as the input, 12 is printed in the console. Why is it so?

Is directly putting in binary possible C++ [duplicate]

This question already has answers here:
Can I use a binary literal in C or C++?
(24 answers)
Closed 9 months ago.
Is putting in binary as a value possible? I want something like char test = 00101011 and it will become 43. I know this is possible by making a function that converts binary to decimal (which can be inputted) but thats not direct and Im pretty sure it takes time.
You need to put the prefix 0b.
#include <iostream>
int main()
{
char c = 0b00101011;
std::cout << static_cast<int>(c) << std::endl;
}

Passing parameters by reference from C++ to x86 MASM [duplicate]

This question already has answers here:
C++ inline assembly: how to deal with references?
(2 answers)
How does assembly do parameter passing: by value, reference, pointer for different types/arrays?
(3 answers)
Passing the reference arguments to an assembly function
(1 answer)
C++ assembler output - how are references implemented
(3 answers)
Closed 11 months ago.
I'm trying to pass parameters by reference from C++ to a function written in assembly, but I can't seem to actually change the value within the assembly function. What am I doing wrong here?
void __declspec(naked) asmChange(int&){
__asm{
mov [esp+4], 5
ret
}
}
int main (){
int a = 12;
cout << "Before: " << a << endl;
asmChange(a);
cout << "After: " << a << endl;
return 0;
}
In the program above I'm simply trying to change the value of a variable. I don't really understand what I'm doing wrong here and can't seem to find any answers to my issue anywhere else online.

Passed array inconsistent bahevior; C++ [duplicate]

This question already has answers here:
Using sizeof on arrays passed as parameters [duplicate]
(3 answers)
What is array to pointer decay?
(11 answers)
Closed 5 years ago.
I'm trying to learn some basic C++ and the moment I started to think I got a grasp of all those pointers I stumbled across this problem:
int sizeOf(string texts[]) {
return sizeof(texts);
}
int main() {
string texts[] = {"apple", "banana", "orange", "watermelon"};
cout << sizeof(texts) << endl;
cout << sizeOf(texts) << endl;
}
and this function returns
128
8
My question is: what is happening when I pass this array as an argument? Why suddenly C++ forgets that this is an array? I have tried to dereference it inside a method (return sizeof(*texts)) but it returned 32 instead which is the size of one string element, not a whole array. Is it possible to do what I want to do? Where am I mistaken?

What's the Difference Between These Two Casts in C++? [duplicate]

This question already has answers here:
Regular cast vs. static_cast vs. dynamic_cast [duplicate]
(8 answers)
What is the difference between static_cast<> and C style casting?
(7 answers)
Closed 9 years ago.
Code sample one:
float i=1.1f;
int j=static_cast<int>(i);
cout << j<< endl;
Code sample Two:
float i=1.1f;
int j=(int)i;
cout << j<< endl;
what's the difference between these code samples about converting in c++?