简体   繁体   English

if()语句使用的条件的目的是什么? C ++

[英]what is purpose of condition used for if() statement? C++

How can I send a NULL pointer from the caller, as any of the three parameters, to the CalcArea function, in the following program? 在下面的程序中,如何将调用者的NULL指针作为这三个参数中的任何一个发送到CalcArea函数? No matter how I modify the condition (not content), of the if () statement, I still see the output from cout << "why am I seeing this..." < 不管我如何修改if()语句的条件(不满足内容),我仍然可以看到cout <<“为什么会看到这个...” <

 #include <iostream>
 using namespace std;

 void CalcArea(const double* const pPi,
               const double* const pRadius,
               double* const pArea)
 {
     if (pPi && pRadius && pArea)
     *pArea = (*pPi) * (*pRadius) * (*pRadius);
     cout<< "why am I seeing this?..." <<endl;
 }

 int main()
 {
     const double Pi = 3.1416;

     cout<< "enter radius of circle: ";
     double Radius = 0;
     cin>> Radius;

     double Area = 0;
     CalcArea (&Pi, &Radius, &Area);

     cout<< "area is = " <<Area<<endl;

     cout<< "press enter to continue..." <<endl;
     cin.ignore(10, '\n');
     cin.get();

     return 0;
 }

Please help me to understand. 请帮助我理解。 It says in my book that "you don't want the function to calculate the area if the caller inadvertently sends a NULL pointer as any of the three parameters", but under what conditions exactly could this occure? 它在我的书中说:“如果调用者无意间将NULL指针作为这三个参数中的任何一个发送,则您不希望函数计算面积”,但是在什么情况下会发生这种情况呢? What is the condition of the if() statement meant to prevent. if()语句要阻止的条件是什么? Remember I am only beginning. 记住我才刚刚开始。 Thank you all, sincerely, newmanadam 谢谢大家,诚挚的,newmanadam

Only the line immediately following the if depends on the evaluation. 仅紧接if之后的行取决于评估。

if (condition)
    statement

If you want both lines to only run if the condition holds, use curly braces. 如果您希望仅在条件成立的情况下才运行两条线,请使用花括号。

if (condition)
{
    multiple
    statements
}

The if condition here simply checks that none of the references you give it are null. 这里的if条件只是检查您提供给它的引用是否都不为空。 This can happen if you explicitly call your function with NULL 如果您使用NULL显式调用函数,则会发生这种情况

CalcArea(&Pi, NULL, &Area);

Or if you are using a pointer, which has previously been set to null. 或者,如果您使用的指针先前已设置为null。 Since you are learning from a book, I would assume it will explain them to you soon enough. 由于您正在学习一本书,因此我认为它会尽快向您解释。

if() works for one statement, in your case for one line: if()适用于一条语句,对于您而言适用于一行:

 if (pPi && pRadius && pArea)
     *pArea = (*pPi) * (*pRadius) * (*pRadius);
 cout << "why am I seeing this?..." <<endl; // cout is outside of if()

you need create block of code to make cout output conditional: 您需要创建代码块以使cout输出有条件:

 if (pPi && pRadius && pArea) {
     *pArea = (*pPi) * (*pRadius) * (*pRadius);
     cout << "why am I seeing this?..." <<endl;
 }

Condition itself is used to check that none of three pointers are equal to nullptr , as dereferencing such pointer would lead to undefined behavior, in simple words to prevent program to crush on invalid input for this function. 使用条件本身来检查三个指针中的任何一个都不等于nullptr ,因为取消引用此类指针会导致未定义的行为,用简单的话来说就是防止程序破坏此函数的无效输入。

You want to brace the whole block which refers to the condition: 您想支撑引用条件的整个块:

void CalcArea(const double* const pPi, 
 const double* const pRadius, 
 double* const pArea)
 {
    if (pPi && pRadius && pArea)
    {
         *pArea = (*pPi) * (*pRadius) * (*pRadius);
         cout << "why am I seeing this?..." <<endl;
         cout << "Because all pointers are != NULL" <<endl;
    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM