简体   繁体   中英

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.

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 :

        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. I think you're looking for an OR check here.

        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.

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 .

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.

The new operator should be used with the delete operator (not with 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.

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.

/* 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. 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;                                                              
}            

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM