在交换一个char * []数组导致问题 [英] Swapping in a char * array[ ] leading to issues

查看:144
本文介绍了在交换一个char * []数组导致问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

void sortArray(char* array[], int count){
int compare;
int index;
 for(index = 0; index < count; index++){ //runs through array, keeping track of index
   for(compare = index; compare < count; compare++){
        if (strcmp(array[compare] , array[index]) <= 0){
             swap(*(array+compare), *(array+index));
        }
   }
 }
}
void swap(char *strA, char *strB){
    char *temp = (char *)malloc((strlen(strA)+1) * sizeof(char));
    assert(temp!=NULL);
    strcpy(temp, strA);
    free(strA);
    strA=(char *)malloc((strlen(strB)+1) * sizeof(char));
    assert(strA!=NULL);
    strA=strcpy(strA, strB);
    free(strB);
    strB= (char *)malloc((strlen(temp)+1) * sizeof(char));
    assert(strB!=NULL);
    strcpy(strB, temp);
    free(temp);
}

给出输出:

Array1[0]: Adam Pendleton
Array1[1]: Jeison Ortega
Array1[2]: Theodore Staake
Array1[3]: Patrick Mahoney
Array1[4]: Andrew Silberstein
Array1[5]: Alan Boulais
Array1[6]: Daniel Comeau
Array1[7]: Sean Mikhael
Array1[8]: Sarah Shank
Array1[9]: Daniel Verge
Array1[10]: Dhimitris Natsis
Array1[11]: Kathleen Lent
Array1[12]: Osei Collins
Array1[13]: Jason Hintlian
Array1[14]: Michael Gibson
Array1[15]: Alex Grossi
Array1[16]: Michael Svedberg
Array1[17]: Randall White
Array1[18]: Alvin Cordor
Array1[19]: Rodrigo Roldan
Array1[20]: Matthew McArthur
Array1[21]: Jesse Anaya
    Sorted Array1[0]: Adam Pendleton
    Sorted Array1[1]: Patrick Mahoney
    Sorted Array1[2]: Theodore Staake
    Sorted Array1[3]: Sarah Shank
    Sorted Array1[4]: Dhimitris Natsis
    Sorted Array1[5]: Alan Boulais
    Sorted Array1[6]: Alex Grossi
    Sorted Array1[7]: Alvin Cordor
    Sorted Array1[8]: Sean Mikhael
    Sorted Array1[9]: Osei Collins
    Sorted Array1[10]: Michael Svedberg
    Sorted Array1[11]: Daniel Comeau
    Sorted Array1[12]: Daniel Verge
    Sorted Array1[13]: Jason Hintlian
    Sorted Array1[14]: Jesse Anaya
    Sorted Array1[15]: Michael Gibson
    Sorted Array1[16]: Matthew McArthur
    Sorted Array1[17]: Randall White
    Sorted Array1[18]: Rodrigo Roldan
    Sorted Array1[19]: Kathleen Lent    <-----not sure why this is dupe
    Sorted Array1[20]:                  <-----not sure why this is here
    Sorted Array1[21]: Kathleen Lent

林不知道在哪里,这些空格都是来自或者为什么有重名。
我甚至创建了一个名为reverseArray另一个函数,只是对调的第一和最后一个字符串,它看起来像这个问题从交换功能茎,我的猜测是,当它被复制字符串有一些乱七八糟的在那里。

Im not sure where these blank spaces are coming from or why there are duplicate names. I have even created another function called reverseArray that just swapped first and last strings and it seems like the issue stems from the swap function, my guess is that when it is copying the strings there is some mess up there.

有关这到底是怎么回事的只是一小部分,我从文件里逐行文本已经阅读以填充的char * []数组与名称的字符串,通过阵列正确打印证明这工作正常之前排序。

For this is just a small portion of what is going on, i have read in text from a file line by line to fill the char* array[] with strings of names, this works fine as evidenced by the array printing correctly before it is sorted.

推荐答案

与code确切的问题是,你的掉期()函数释放两个指针传递到它,并且不返回指针以任何形式分配新的内存。这样一来,所有的数组中的指针你整理落得指向已经被释放的内存 - 这是一个奇迹,它的作品在所有

The exact problem with your code is that your swap() function is freeing the two pointers passed into it, and does not return pointers to the new memory it allocates in any form. As a result, all of the pointers in the array you're sorting end up pointing to memory that has been freed -- it's a wonder that it works at all.

要解决这个问题,你可以修改的签名交换()参照拿两个指针:

To fix this, you could change the signature of swap() to take the two pointers by reference:

void swap (char **strA, char **strB)

在将它传递指针为:

and pass pointers in to it as:

swap(array+compare, array+index);

话虽这么说,Claudiu的建议,你只要将指针而不是拷贝数据周围可能是从长远来看,一个更好的主意。 :)

That being said, Claudiu's suggestion that you just swap pointers instead of copying data around is probably a much better idea in the long run. :)

这篇关于在交换一个char * []数组导致问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆