I am trying to create a function called getSortedRanks which returns an array. I copied the format for returning arrays from this question Return array in a function but the array is not being returned correctly.
#include <stdlib.h>
#include <stdio.h>
#define familyMembers 4
int *getSortedRanks()
{
int rankedMembers[familyMembers] = {3,4,2,1};
return rankedMembers;
}
int main()
{
int *sortedRanks = getSortedRanks();
//print the returned array
for(int i = 0; i < familyMembers; i ++)
{
cout << "ranked member is " << sortedRanks[i] << endl;
}
return 0;
}
When I run this the output is:
ranked member is 3
ranked member is 0
ranked member is 0
ranked member is 2686744
The first element of the array sortedRanks is always correct but the others are not. How can I correct the way the array is being returned?
const