C++ Note 15: Extension of C++ to C - Enhancement of Ternary Operators

How the ternary operator behaves in a C compiler:

intmain()

{

int a=10;

int b=20;

//The ternary operator is an expression, the expression cannot be an lvalue

(a<b?a:b)=30;

 

printf("a=%d,b=%d",a,b);

return 0;

}

 

Running the program we found that the compilation failed!

Looking at the reason we see that expressions cannot be lvalues .

How to understand this sentence?

In the C language, the return value of an expression is a number, and the operation result of the expression is placed in the register of the CPU, not in the memory, not a memory address (check the notes for the lvalue and rvalue of C and C++ later) .

That is to say, in the C language, a=10, b=20, and executing the a<b?a:b statement is [10<20? Yes! good! Then return the value of a! ] returns the number "10", then 10=30, assign 30 to 10? What does this mean, it went wrong!

In C++, the expression returns the variable itself, which is the returned variable a.

Compile the same code in C++:

#include<iostream>

using namespace std;

 

intmain()

{

int a=10;

int b=20;

//The ternary operator is an expression, the expression cannot be an lvalue

(a<b?a:b)=30;

 

printf("a=%d,b=%d",a,b);

 

system("pause");

return 0;

}

operation result:

a=30,b=20

Please press any key to continue...

 

So how to achieve the same effect in C language?

Let's think about it, if we want the expression to be an lvalue, that means the ternary expression should return a memory space ! That is, the first address of the memory! What is the first address of memory? Just a pointer.

After the expression a<b ? a:b is finished, I don't want it to return the value of a, 10. If I can return the address of a, and then assign a value to this address, that's fine.

I can write this: (a < b ? &a : &b) = 30, through this address, the memory space pointed to by the pointer can be indirectly modified, and the value of a can be indirectly modified. Note that an asterisk "* is added before the parentheses. " is to modify the corresponding value. After we modify it and run it, we find that it is the same as C++ running.

intmain()

{

int a=10;

int b=20;

//The ternary operator is an expression, the expression cannot be an lvalue

*(a<b?&a:&b)=30;

 

printf("a=%d,b=%d",a,b);

return 0;

}

operation result:

a=30,b=20

Press any key to continue

 

Therefore, the C++ compiler is to help our programmers complete the work of taking the address.

Conclusion :

1. The C language returns the value of the variable, and the C++ language returns the variable itself;

The ternary operator in C language returns the variable value and cannot be used as an lvalue;

The ternary operator in C++ returns the variable itself directly, so it can appear anywhere in the program.

2. Note: If one of the values ​​returned by the ternary operator is a constant value, it cannot be used as an lvalue, such as

(a < b ? 1 : b)= 30;

3. How does C language support features similar to C++?

When the condition of the lvalue: there is memory space, the C++ compiler helps the programmer to take an address.

 

 

 

 

 

long press to unlock

 

Unlock more exciting insider

 

programming according to the law

WeChat: Lightspeed-Tech

Technology drives life

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325061116&siteId=291194637