2015-11-13 39 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; 
相關問題