将指针设置为函数的返回值

Setting pointer to the return value of a function

本文关键字:返回值 函数 指针 设置      更新时间:2023-10-16

我正在尝试设置变量 char * vowels and char * consonants 作为函数搜索vowels和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 中,删除操作员只能用于指向使用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] = '';
    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] = '';
                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 */
}

我解决了问题,它正在返回,但没有设置数据应该如何。我需要使用||元音的管道,并为循环设置另一个以检查所有条件。它与释放记忆无关。这是功能的代码。

     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;                                                              
}