동적 메모리 할당 맞는지 확인좀 해주세요!!
감추어왔던
#includeiostream
#includecstring
using namespace std;char *sort(char * s);
void sort(char *const s, char * s1);int main()
{
char word[] = garden;
coutThe original string is wordendl;
coutThe after string is sort(word)endl;
for(int i=0; i1; i++)
cout&word[i];
char s1[7];
char s[] = garden;
cout The string in s is sendl;
cout The string in s1 is ;
sort(s,s1);
couts1 ;
system(PAUSE);
return 0;
}
char *sort(char * s)
{
char *word= new char[7]; for (int i = strlen(s) - 1; i = 1; i--)
{
char currentMax = s[0];
int currentMaxIndex = 0;
for (int j = 1; j = i; j++)
{
if (currentMax s[j])
{
currentMax = s[j];
currentMaxIndex = j;
}
}
if (currentMaxIndex != i)
{
s[currentMaxIndex] = s[i];
s[i] = currentMax;
}
}
}void sort(char * const s, char * s1)
{
for(int i=0; i7; i++)
{
s1[i]= s[i];
}}제가 위에 빨간색으로 표시한 부분을 적어서 동적메모리를 할당해서 포인터값으로 반환했는데 제대로 된거 맞나요?? 컴파일은 되는데 제가 제대로 작성한건지 알고 싶어요!!
-
먹딸기
맞아요.