简体   繁体   English

指定指针的指针

[英]Assigning pointers to pointers

I have pointer to pointers to char set as: 我有指向char set指针的指针:

    char **A;
    char **B;

I am making string sorting using Radix-Sort, and i have to sort it a few times, but if I sort it, it's just sorting array A and saves the result in array B . 我正在使用Radix-Sort进行字符串排序,我必须对它进行排序几次,但如果我对它进行排序,它只是对数组A排序并将结果保存在数组B What I want to do, is to assign A to be B , which is already almost-sorted A , but when I do: 我想要做的是将A分配给B ,这已经是几乎已经排序的A ,但是当我这样做时:

    A = B;

I just set the pointers, right? 我只是设置了指针,对吗? Which means A points now at B . 这意味着A现在在点B And, when I later reassign values I have multiple results of same string. 而且,当我稍后重新分配值时,我有多个相同字符串的结果。 For example: 例如:

    A = {"aa", "ba", "bc", "bd"};

After sorting and using A = B , the output looks like: 排序并使用A = B ,输出如下:

    B = {"aa", "aa", "bc", "bd"};

I also tried using memcpy() - which should copy what B points at to place that is pointed by A , correct? 我使用也尝试过memcpy() -这应该复制什么B由指示的点的地方来A ,对不对? But result is the same. 但结果是一样的。 No idea how to get this to work. 不知道如何让这个工作。 Wasn't sure whether to post here full code, so I uploaded it on pastebin . 不确定是否在这里发布完整代码,所以我将它上传到pastebin上

Thanks for any help in advance. 在此先感谢您的帮助。

Edit2: With a bit help, changed it and it works fine with memcpy() , but it still doesn't 'stable' sorts, and instead of B = {"bb", "bc"} I have the opposite: B = {"bc", "bb"} . 编辑2:有点帮助,改变了它,它与memcpy()工作正常,但它仍然没有'稳定'排序,而不是B = {"bb", "bc"}我有相反的结果: B = {"bc", "bb"} I'm still fighting it, but with no results... yet. 我还在打架,但还没有结果......

void CSort(int p){

int k = 123;
int C[k];
int i;

for(i = 0; i <= k; i++){
    C[i] = 0;
}

for(i = 0; i < ILE; i++){
    C[(int)As[i][p]]++;
}

for(i = 1; i <= k; i++){
    C[i] = C[i] + C[i - 1];
}

for(i = 0; i < ILE; i++){     // ile means how many words there are
    Bs[C[(int)As[i][p]] - 1] = As[i];
    printf("As[%i][%i] == %c ", i, p, As[i][p]);
    printf("C[%i] == %i ", (int)As[i][p], C[(int)As[i][p]]-1);
    printf("  Bs[%i]  == %s \n", C[(int)As[i][p]] - 1, Bs[C[(int)As[i][p]] - 1]);

    //(As[i], Bs[C[(int)As[i][p]]], sizeof(As[i]));
    C[(int)As[i][p]]--;
}


}

This is my Counting Sort, and here comes my radix: 这是我的Counting Sort,这是我的基数:

void RSort(int d){
    int i;
    for(i = d; i >= 0; i--){
        CSort(i);
        memcpy(As, Bs, sizeof(*As) * ILE);
    }
}

I haven't even got idea - why - because on paper, it works just fine! 我甚至没有想法 - 为什么 - 因为在纸上,它工作得很好!

To fix my problem, all i had to do, was to change order in last loop: 要解决我的问题,我必须做的就是在最后一个循环中改变顺序:

for(i = 0; i < ILE; i++)

change to 改成

for(i = ILE - 1; i >= 0; i--)

And everything works just fine! 一切正常!

When you assign 当你分配

 A = B;

you assign A to point to the same memory B points at, not A to point at B . 你指定A指向相同的存储器B指向,而不指向A指向B

In order to copy the array you would need to allocate memory to hold it and memcpy all contents from B to A 为了复制数组,您需要分配内存来保存它并将所有内容从B记忆到A

char ** A = malloc(sizeof(*A) * numitems);
memcpy(A, B, sizeof(*A) * numitems);

Then if you sort A or B it will not affect the other because you have copies. 然后,如果您对AB排序,它将不会影响另一个,因为您有副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM