c++ - 为什么变量 myOperator 没有显示内存位置,为什么没有正确传递

标签 c++ function pointers

我尝试过以多种方式调试它。在我的一般假设中,我认为这可能是 tempOperator 的范围界定问题。我的作业指出我需要一个通过指针传递的示例,并且在我使用的通过指针传递的部分中我不能也通过地址传递。我知道 GetNumber 函数和 Printing 函数工作得很好。指针坏了,我不太确定是什么。目标是创建指向 myOperator 的指针。将其传递到函数中并在该函数中更改 myOperator。

#include <iostream>
#include <math.h>
#include <string>

using namespace std;

void GetNumber    (float[], int); // prototype GetNumber function that accepts a float array data type.
char* GetOperator  (char*);       // prototype GetOperator to accept and return a copy of a poiunter.
void PrintProblem (float[], char, string&, float&); // Prototpe PrintProblem function to accept an array, character, string, and float address.

int main()
    {

    //------------------------------Declare Variables----------------
     float storageBox [2];                                           // Declare an array contraing to floats.
     float result;                                                   // Declare a float variable named result.
     int functionCounter = 0;                                        // Declare an integer variable named functionCounter and initialize it to 0.
     char myOperator;                                                // Declare a character variable data type and name is 
     string operatorType ;                                           // Decare a string variable and name is operatorType. 
     char* pOperator = NULL;  
    //------------------------------Body-----------------------------
     cout << "The address myOperator is: " << &myOperator << endl << endl;        // View the Address of myOperator
     GetNumber (storageBox, functionCounter);                 // Acquires a value and stores is in the array slot = functionCounter.
     functionCounter += 1;                                    // Make functionCounter equivalent to 1 more than it's previous value.
     pOperator = &myOperator;                                 // Make pOperator hold the Address of myOperator, and point to myOperator.
//   ***********************************************************************
//   Debugging Section - (Conclusion - myOperator isn't getting a memory location?)
//   ***********************************************************************
     cout << endl << "The address of pOperator is: " << &pOperator << endl;       // View the Address of pOperator
     cout << "The address myOperator is: " << &myOperator << endl << endl;        // View the Address of myOperator
//   ********************End Debug*****************************************

     GetOperator (pOperator);                  // Make a call to the getOperator function and pass it a copy of the pointer pOperator. 

//   ***********************************************************************
//   Debugging Section - (Something breaks)
//   ***********************************************************************
     cout << "The value stored in the location pOperator points to is : " << *pOperator << endl;         // View the contents of pPointer.
     cout << "The value of myOperator is: " << "\n\n" << myOperator;                                     // View the contents of myOperator.
//   ********************End Debug*****************************************
     GetNumber (storageBox, functionCounter);                    // Acquires a value and stores is in the array slot = functionCounter.
     PrintProblem(storageBox, myOperator, operatorType, result); // Prints the outcome

     return 0;                                              
    }
// First Function - pass by refference (will grab a single Number)
void GetNumber (float storageBox[], int functionCounter)      //(Functioning properly)
 {
  float tempNumber;                                       

     cout << "Enter a number : ";      
     cin  >>  tempNumber;              
     storageBox[functionCounter] = tempNumber;             // fills the array slot functionCounter represents with tempNumber
 }
// pass by pointer to obtain the operator and problem type.
char* GetOperator (char* pOperator)
 {
  char tempOperator;

  cout << "Please enter a mathematical operator ( +, -, *, /): ";
  cin  >> tempOperator;
  pOperator = &tempOperator;                              // set the copy of pOperator to the adress of tempOperator

//   ***********************************************************************
//   Debugging Sectopn- (Functional)
//   ***********************************************************************
  cout << "\nThe value found in pOperator is : " << *pOperator << endl;      // output the contect of the memory location pOperator points to.(tempOpertor)
//   ********************End Debug*****************************************

  return (pOperator); 
 }
// Everything beyond this point functions properly.
// pass by copy on output   
void PrintProblem (float storageBox[2], char myOperator, string& operatorType,
                   float& result)
 {
    switch (myOperator)
     {
      case '+':
         {
           operatorType = "Addition: ";
           result = storageBox[0] + storageBox[1];
           break;
         }
      case '-':
         {
           operatorType = "Subtraction: ";
           result = storageBox[0] - storageBox[1];
           break;
         }
      case '*':
         {
           operatorType = "Multiplication: ";
           result = storageBox[0] * storageBox[1];;
           break;
         }
      case '/':
         {
           operatorType = "Division: ";
           result = storageBox[0] / storageBox[1];
           break;
         }
      default:
         {
            cout << "\nYour operator is invalid!\n\n";
         }
     } 

  cout << operatorType  << storageBox[0] << " " << myOperator << " " 
       << storageBox[1] << " " << " = "        << result;
 }

最佳答案

GetOperator 是“颠倒”的——你不应该把局部变量的地址赋给参数,你应该把局部变量的值赋给参数指向的变量:

*pOperator = tempOperator;  

关于c++ - 为什么变量 myOperator 没有显示内存位置,为什么没有正确传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33686948/

相关文章:

c++ - "Static"类成员

c++ - 调用删除时的总线错误 (C++)

c++ - 奇怪的 WMI 查询结果

c# - 类内函数的层次结构 (C#)

C - 如何创建指向不同数据类型的数组?

c++ - 无法从 std::shared_ptr<_Ty> 转换为 std::shared_ptr<_Ty>

c++ - rapidjson pretty print 使用 JSON 字符串作为编写器的输入

python - 在Python中为游戏添加计分时钟

c - 使用指针通过函数传递数组

c++ - 为什么 std::function::argument_type 已被弃用?