我想通過交換指針來對一個char指針(char * _string)數組進行排序。通過交換指針,C++對char數組排序char
我有這個方法,我想要做的就是使用我從_string得到的值,並且不通過操作_string,而是對我將該方法交給的空幫助程序數組(char * _output)進行排序。
任何人都可以幫助我,告訴我我做錯了什麼嗎?
void sortAsc(char* _string, char* _output)
{
int length = strlen(_string);
// output and string now point to the same area in the memory
_output = _string;
for(int i = 0; i < length; i++) {
for(int j = 0; j < length; j++) {
if(*(_output) > (_output[j])) {
// save the pointer
char* tmp = _output;
// now output points to the smaller value
_output = _output+j;
// move up the pointer to the smaller value
_output + j;
// now the pointer of the smaller value points to the higher value
_output = tmp;
// move down to where we were + 1
_output - j + 1;
}
}
}
//_output[length]='\0';
//delete chars;
}
在我的主法,我做這樣的事情:
char * string = {"bcdae"};
char * output = new char[5];
sortAsc(string, output);
代碼後,我想輸出數組包含排序值。
這是什麼問題呢?爲我們提供一些示例輸入和輸出或您遇到的錯誤。 –
'_output = _string'之後,您將失去對傳遞給函數的幫助程序數組的引用,當然這沒有幫助。 – Jack
編譯後輸出變量應該是「abcde」。 此時,程序不會拋出任何錯誤,但調用該方法後輸出變量仍爲空。 –