繁体   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