我想通过交换指针来对一个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”。 此时,程序不会抛出任何错误,但调用该方法后输出变量仍为空。 –