简体   繁体   中英

having trouble returning an array from a function c++

#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
using namespace std;

void getinput (string &first,string &second);
void lengthcheck (string first, string second);
//int anagramcheck (string word);
int* lettercounter (string input);

int main()
{
    std::string a;
    std::string b;
    getinput(a,b);
    lengthcheck (a,b);
    lettercounter(a);
    lettercounter(b);

    int* one = lettercounter(a);
    int* two = lettercounter(b);

    if (one == two)
        cout << "You Have Entered An Anagram" << endl;
    else
        cout << "You Have Not Entered An Anagram" << endl;
}

void getinput (string &first, string &second) {
    cout << "Enter First Input: ";
    getline(cin, first, '\n');
    cout << "Enter Second Input: ";
    getline(cin, second, '\n');
    cout << "You Entered " << first << " and " << second <<endl;
}

void lengthcheck(string first, string second){
    int lengtha = first.length();
    int lengthb = second.length();
    
    if ((lengthb > 60) || (lengtha > 60)) {
        cout << "Input Is Invalid" << endl;
    } else if (lengtha !=lengthb) {
        cout << "Input is not an anagram" << endl;
    } else {
        cout << "Input is Valid" << endl;
    }
}

int* lettercounter(string input)
{
    static int freq[26] = {0};
    int length = input.length();
    for (int i=0; i<26; i++) {
        freq[i]=0;
    }
    
    for (int i=0; i <length; i++) {
        if(input[i]>='a' && input[i]<='z')
        {
            freq[input[i] - 97]++;
        }
        else if(input[i]>='A' && input[i]<='Z')
        {
            freq[input[i] - 65]++;
        }
    }
    
    for(int i=0; i<26; i++) {
        /* If current character exists in given string */
        if(freq[i] != 0)
        {
           printf("'%c' = %d\n", (i + 97), freq[i]);
        }
        return freq;
    }
}

I am having trouble returning the array named freq from the user definied function called lettercount . Can someone give me a hint? I need the lettercount to return an array. I need to call the function lettercount twice so i can compare the results of each array to determine if the two inputs are anagrams. I am not sure if the function is returning an actual value to the main.

First of all, freq shouldn't be static. By making it static, you would be accessing the same array everytime. For what you want to do, you don't want to always access the same memory.

In second place, you cannot just return a pointer to memory that has not being allocated dynamically or that isn't static. When you get out of scope (ie you return from the function lettercounter back to main ), the memory that was occupied by the array will be freed. So, you would be returning a pointer to memory that is no longer reserved, resulting in undefined behavior.

If you really need to work with raw pointers, then each time you enter lettercounter , you would need to allocate memory for the array dynamically like this: int * freq = new int[26]; . This will reserve memory for an array of size 26. Then, when you return freq, the memory will still be allocated. However, don't forget that the memory allocated with new doesn't delete itself. You have to clean your mess. In this case, at the end of main you would call delete[] one; and delete[] two; .

int* lettercounter(string input)
{
  int * freq = new int[26];
    .
    .
    .
  return freq;
}

int main()
{
  .
  .
  int* one = lettercounter(a);
  int* two = lettercounter(b);
  .
  .
  delete[] one;
  delete[] two;
}

In any case, I'd recommend you to learn to use smart pointers and about standard containers (like a vector ). These operations would be much simpler.

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