Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么不是';t显示内存位置的变量myOperator,以及为什么未正确传递_C++_Function_Pointers - Fatal编程技术网

C++ 为什么不是';t显示内存位置的变量myOperator,以及为什么未正确传递

C++ 为什么不是';t显示内存位置的变量myOperator,以及为什么未正确传递,c++,function,pointers,C++,Function,Pointers,我试过用多种方法调试它。在我的一般假设中,我认为这可能是tempOperator的范围界定问题。我的赋值说明我需要一个按指针传递的示例,在我使用的按指针传递的部分中,我也不能按地址传递。事实上,我知道GetNumber函数和Printing函数工作得很好。指针坏了,我不确定是什么。目标是创建指向myOperator的指针。将其传递到函数中,并在该函数中更改myOperator #include <iostream> #include <math.h> #include &

我试过用多种方法调试它。在我的一般假设中,我认为这可能是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;
 }
#包括
#包括
#包括
使用名称空间std;
void GetNumber(float[],int);//接受浮点数组数据类型的原型GetNumber函数。
char*GetOperator(char*);//prototype GetOperator接受并返回poointer的副本。
无效打印问题(float[],char,string&,float&);//Prototpe PrintProblem函数,用于接受数组、字符、字符串和浮点地址。
int main()
{
//------------------------------声明变量----------------
float-storageBox[2];//声明一个与float相反的数组。
float result;//声明名为result的浮点变量。
int functionCounter=0;//声明一个名为functionCounter的整数变量,并将其初始化为0。
char myOperator;//声明字符变量数据类型,名称为
string operatorType;//删除字符串变量,名称为operatorType。
char*pOperator=NULL;
//------------------------------身体-----------------------------

cout
GetOperator
为“反向”-不应将局部变量的地址分配给参数,应将局部变量的值分配给参数指向的变量:

*pOperator = tempOperator;  

为什么GetOperator函数声明为返回char*

当前,GetOperator函数正在将本地参数的地址分配给pOperator函数参数。您可能希望将本地参数分配给pOperator所指向的对象-也就是说,您有以下行:

pOperator = &tempOperator;
您可能想要:

*pOperator = tempOperator;
编辑: 下面是一个比较简单的例子:

#include <iostream>

void SetValueNotWorking(int* i) {
    int bar = 42;

    // Sets the function parameter 'i' to the address of 'bar'
    i = &bar;
}

void SetValueWorking(int* i) {
    int bar = 42;

    // Sets the int pointed-to by function parameter 'i' to the value of 'bar'
    *i = bar;
}

int main(int argc, char** argv) {
    int foo = 0;

    SetValueNotWorking(&foo);
    std::cout << foo;    

    SetValueWorking(&foo);
    std::cout << foo;

    return 0;
}
#包括
无效SetValueNotWorking(int*i){
int bar=42;
//将函数参数“i”设置为“bar”的地址
i=&bar;
}
无效SetValueWorking(int*i){
int bar=42;
//将函数参数“i”指向的int值设置为“bar”
*i=巴;
}
int main(int argc,字符**argv){
int-foo=0;
SetValueNotWorking(&foo);

我真的很感谢大家。我真不敢相信我没有早点看到这一点。这很简单,但我很容易就忽略了。你太棒了。