简体   繁体   中英

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? 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..." <

 #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? What is the condition of the if() statement meant to prevent. Remember I am only beginning. Thank you all, sincerely, newmanadam

Only the line immediately following the if depends on the evaluation.

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. This can happen if you explicitly call your function with NULL

CalcArea(&Pi, NULL, &Area);

Or if you are using a pointer, which has previously been set to 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 (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:

 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.

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;
    }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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