Returning address of local variable Warning

Hi everyone~

Just wondering if someone can help me get rid of this compiler warning? On the "return a" line I've got warning C4172: returning address of local variable or temporary

So in one function I've got:
 
int* givenStringBucket = this->BucketSortString("Hello");


Then
1
2
3
4
5
6
7
8
int* BucketSortString(string s) {
	int a[26] = { 0 };
	for (size_t i = 0; i < s.size(); i++) {
		int num = static_cast<int>(s[i]) - 97;
		a[num] = a[num] + 1;
	}
	return a;
}


Thanks!
int a[26] = { 0 }; This is a local variable. That meant, it will be destroyed as soon as function ends.
Line 7: you are trying to retun its address.

You need either a manually manage memory, or use something automatic. In your cae returning an std::array or std::vector is the best choice.
Topic archived. No new replies allowed.