简体   繁体   English

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

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

I'm trying to set the variables char * vowels and char * consonants as the return value of the functions searchVowels and searchConsonants respectively. 我正在尝试将变量char *元音char *辅音分别设置为函数searchVowels和searchConsonants的返回值。

Although when I test the code, the above variables are getting set properly but not being passed back into the main. 尽管在测试代码时,上述变量已正确设置,但没有传递回主变量。 And during a test with 并在与

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

it doesn't display the consonant value now. 它现在不显示辅音值。

Here's my code, any suggestions would be super helpful. 这是我的代码,任何建议都将非常有帮助。 Except that I can't use strings.(For a class) 除了我不能使用字符串。(对于一个类)

Also is this the appropriate way to post code? 这也是发布代码的合适方法吗?

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

 }    

In the method searchVowels , you seems to have the following code : searchVowels方法中,您似乎具有以下代码:

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

How are you expecting the arrayCopy[i] to pass the check since it cannot have all vowels at the same time. 您如何期望arrayCopy[i]通过检查,因为它不能同时具有所有元音。 I think you're looking for an OR check here. 我认为您正在此处寻找OR检查。

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

In the above case, it might fill the arrayVowels with something if the check passes. 在上述情况下,如果检查通过,它可能会用一些东西填充arrayVowels

Also, you can make the above code into a function something like HasVowels() , which checks if the the arrayCopy has a vowel at the ith index and then use it in both searchVowels and searchConsonants . 另外,您可以将上面的代码变成类似HasVowels()的函数,该函数检查arrayCopy在第ith个索引处是否有元音,然后在searchVowelssearchConsonants使用它。

One more thing is the usage of "free" in your code. 还有一件事是在代码中使用“免费”。 In C++, delete operator should only be used either for the pointers pointing to the memory allocated using new operator , and free() should only be used either for the pointers pointing to the memory allocated using malloc() or for a NULL pointer. 在C ++中,delete运算符仅应用于指向使用new operator分配的内存的指针,而free()仅应用于指向使用malloc()分配的内存的指针或NULL指针。

The new operator should be used with the delete operator (not with free ). new运算符应与delete运算符一起使用(而不是free )。

You should not delete or free memory that you're returning from a function if the return value is intended for use by the caller. 如果返回值是供调用者使用的,则不应删除或释放从函数返回的内存。

In this example below, the caller of the function (not the function itself) allocates the memory and the caller of the function frees the memory when it is no longer needed. 在下面的示例中,函数的调用者(而不是函数本身)分配内存,并且当不再需要该函数的调用者时,将释放该内存。

The searchVowels function won't necessarily know when the caller no longer needs the memory. 当调用者不再需要内存时, searchVowels函数不一定会知道。

In this example below, the searchVowels function does not allocate memory and assumes that memory for the dest argument array and the input string have already been allocated. 在下面的此示例中, 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 */
}

I fixed the issue, it was returning but not setting the data how it should have been. 我解决了这个问题,它正在返回但未按原样设置数据。 I needed to use the || 我需要使用|| pipes for the vowels and set up another for loop to check all the conditions. 用于元音的管道,并设置另一个for循环以检查所有条件。 It had nothing to do with freeing up memory. 它与释放内存无关。 Here's the code of the function. 这是函数的代码。

     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