2015-11-13 35 views
1

我试过用多种方式调试它。在我的一般假设中,我认为这可能是tempOperator的一个范围问题。我的任务指出,我需要有一个通过指针传递的例子,并且在我使用指针传递的部分中,我不能通过地址传递。我知道GetNumber函数和打印功能完美地工作。有些东西与指针断裂,我不确定它是什么。目标是创建一个指向myOperator的指针。将它传递给一个函数并在该函数中更改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; 
} 

回答

0

为什么你的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; 
} 
+0

谢谢大家。我不敢相信我没有看到这一点。这很简单,但我很容易忽略它。你真棒。 –

2

GetOperator被“逆转” - 你不应该局部变量的地址分配给参数,你应该局部变量的值参数点分配给变量:

*pOperator = tempOperator; 
相关问题