簡體   English   中英

將指針設置為函數的返回值

[英]Setting pointer to the return value of a function

我正在嘗試將變量char *元音char *輔音分別設置為函數searchVowels和searchConsonants的返回值。

盡管在測試代碼時,上述變量已正確設置,但沒有傳遞回主變量。 並在與

cout << "text vowels" << vowels << "sametext" << consonants; ///something like this.

它現在不顯示輔音值。

這是我的代碼,任何建議都將非常有幫助。 除了我不能使用字符串。(對於一個類)

這也是發布代碼的合適方法嗎?

 #include <iostream>                                                             
 #include <cctype>                                                               
 #include <cstring>                                                                   
 using namespace std;                                                                                                                                         
 const int SIZE = 7;                                                             


 //This function greets the user.                                                
 void greetUser();                                                               

 //This funcion asks for the array of letters.                                   
 char * inputLetters(char * inputArray);                                         

 //This will capitalize all letters to make them easier for the computer         
 //to compare.                                                                   
 char * capitalizeLetters(char * inputArray);                                    

 //This function will search the array for vowesl. If no vowels no game.         
 char * searchVowels(char * arrayCopy);                                          

 ///This function will search the array for consonants.                          
 char * searchConsonants(char * arrayCopy);                                      


 //This capitalizes all the letters in the initial array.                        
 char * capitalizeLetters(char * inputArray)                                     
 {                                                                               
    for (int i = 0; i < 6; ++i)                   
                    {                                                                       
            inputArray[i] = toupper(inputArray[i]);                         
    }                                                                       
 //      inputArray = toupper(inputArray);                                       
    return inputArray;                                                      
 }                                                                               


 //This program will search the array for consonants                             
    //and return the consonants array.                                      
 char * searchConsonants(char * arrayCopy)                                       
 {                                                                               

    char * consonants; consonants = new char[SIZE];                         

    for (int i = 0; i < 6; ++i)                                             
    {//I feel like I could make this into a function itself...hmm           
            if( arrayCopy[i] != 'A' && arrayCopy[i] != 'E'                  
            && arrayCopy[i] != 'I' && arrayCopy[i] != 'O' &&                
            arrayCopy[i] != 'U' && arrayCopy[i] != 'Y')                     
            {                                                               
            consonants[i] = arrayCopy[i];                                   
            }                                                               
    }                                                                       

 return consonants;                                                              

 }    

searchVowels方法中,您似乎具有以下代碼:

        if( arrayCopy[i] == 'A' && arrayCopy[i] == 'E'                  
        && arrayCopy[i] == 'I' && arrayCopy[i] == 'O' &&                
        arrayCopy[i] == 'U' && arrayCopy[i] == 'Y')                     
        {                                                               
                arrVowels[i] = arrayCopy[i];                            
        }

您如何期望arrayCopy[i]通過檢查,因為它不能同時具有所有元音。 我認為您正在此處尋找OR檢查。

        if( arrayCopy[i] == 'A' || arrayCopy[i] == 'E'                  
        || arrayCopy[i] == 'I' || arrayCopy[i] == 'O' ||
        arrayCopy[i] == 'U' || arrayCopy[i] == 'Y')                     
        {                                                               
                arrVowels[i] = arrayCopy[i];                            
        }

在上述情況下,如果檢查通過,它可能會用一些東西填充arrayVowels

另外,您可以將上面的代碼變成類似HasVowels()的函數,該函數檢查arrayCopy在第ith個索引處是否有元音,然后在searchVowelssearchConsonants使用它。

還有一件事是在代碼中使用“免費”。 在C ++中,delete運算符僅應用於指向使用new operator分配的內存的指針,而free()僅應用於指向使用malloc()分配的內存的指針或NULL指針。

new運算符應與delete運算符一起使用(而不是free )。

如果返回值是供調用者使用的,則不應刪除或釋放從函數返回的內存。

在下面的示例中,函數的調用者(而不是函數本身)分配內存,並且當不再需要該函數的調用者時,將釋放該內存。

當調用者不再需要內存時, searchVowels函數不一定會知道。

在下面的此示例中, searchVowels函數未分配內存,並假定已經分配了dest參數數組和輸入字符串的內存。

/* IMPORTANT: This code assumes that SIZE is already defined */

void searchVowels(char* dest, char * inputString) {                                          
    int destIndex;
    int i;

    /* Update this function with other functionality */
    /* as intended */

    destIndex = 0;
    dest[destIndex] = '\0';

    for(int i = 0; i < strlen(inputString); i++)                                              
    {                                                                       
        if ((inputString[i] == 'A') || (inputString[i] == 'E')) {
        {   
            if (destIndex < SIZE) {
                dest[destIndex] = inputString[i];
                dest[destIndex+1] = '\0';
                destIndex = destIndex + 1;
            }                                                            
        }                                                               
    }                                                                       
}  

/* From the code that calls searchVowels */

char* result;

try {
    char* result = new char[SIZE];

    searchVowels(result, "TESTAEIOU");

    /* Use the result variable Here */

    delete result;

} catch (std::bad_alloc&) {
  /* new did not allocate memory */
}

我解決了這個問題,它正在返回但未按原樣設置數據。 我需要使用|| 用於元音的管道,並設置另一個for循環以檢查所有條件。 它與釋放內存無關。 這是函數的代碼。

     char * searchConsonants(char * arrayCopy)                                       
{                                                                               

    char * consonants; consonants = new char[SIZE];                         

    for (int i = 0; i < 6; ++i)                                             
    {//I feel like I could make this into a function itself...hmm           
            for(int j = 0; j < 6; ++j)                                      
            {                                                               
                    if( arrayCopy[j] != 'A' && arrayCopy[j] != 'E'          
                    && arrayCopy[j] != 'I' && arrayCopy[j] != 'O' &&        
                    arrayCopy[j] != 'U' && arrayCopy[j] != 'Y')             
                    {                                                       
                    consonants[i] = arrayCopy[j];                           
                    ++i;                                                    
                    }                                                       
            }                                                               
    }                                                                       

return consonants;                                                              
}            

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM