简体   繁体   English

使用strcpy交换元素在数组中不起作用

[英]Swapping the element using strcpy not working in the array

I was trying to swap the elements in the array (which is extremely basic, but when I try to change the index of the array, it mess up the whole array) 我试图交换数组中的元素(这是非常基本的,但是当我尝试更改数组的索引时,它会使整个数组弄乱了)

#include <string.h>
#include <stdio.h>
int     main(int argc, char **argv)
{
    int swap;
    int position;
    char temp[1000];
    char *i;
    swap = 1;

    while (swap == 1)
    {
        swap = 0;
        position = 1;
        while (position < (argc - 1))
        {
            if (strcmp(argv[position], argv[position + 1]) > 0)
            {
                strcpy(temp, argv[position + 1]);
                strcpy(argv[position], argv[position + 1]);
                strcpy(argv[position + 1], temp);
            }
            position++;
        }
    }
    return (0);
}

from the code above, if I put "hi" and "hello", instead of printing "hello" "hi", it brings "hello" "lo" which is super bizarre. 从上面的代码中,如果我输入“ hi”和“ hello”,而不是打印“ hello”“ hi”,则会带来“ hello”“ lo”,这非常奇怪。

However, when I just simply use pointer (the code below), it works flawlessly. 但是,当我仅使用指针(下面的代码)时,它就可以完美地工作。

#include <string.h>
#include <stdio.h>
int     main(int argc, char **argv)
{
    int swap;
    int position;
    char temp[1000];
    char *i;
    swap = 1;

    while (swap == 1)
    {
        swap = 0;
        position = 1;
        while (position < (argc - 1))
        {
            if (strcmp(*(argv + position), *(argv +position + 1)) > 0 )
            {
                i = *(argv + position);
                *(argv + position) = *(argv + (position + 1));
                *(argv + position + 1) = i;
                swap = 1;
            }
            position++;
        }
    }
    return (0);
}

could you tell me what is wrong with the first way of doing? 你能告诉我第一种做法有什么问题吗?

The arguments, to which the argv vector points, is usually structured like this: argv向量指向的参数通常是这样构造的:

"foo\0barbaz\0quux\0"

with argv[1] pointing to the "f", argv[2] pointing to "b" and argv[3] pointing to "q". argv[1]指向“ f”, argv[2]指向“ b”, argv[3]指向“ q”。

Now you can easily see, that you mess up the arguments and the pointers to them if you swap these naively; 现在,您可以轻松地看到,如果天真地交换了参数和指向它们的指针,您会感到混乱; for example, if you swap "foo" and "barbaz", it looks like this: 例如,如果交换“ foo”和“ barbaz”,则如下所示:

"barbaz\0foo\0quux\0".

Fine, but now argv[2] still points to the same location, which is the second "a" in barbaz! 很好,但是现在argv[2]仍指向同一位置,这是barbaz中的第二个“ a”!

Your first method is even more flawed since you use the pointers from the argv vector as destinations. 第一种方法的缺陷更大,因为您将argv向量中的指针用作目标。 First, you copy foo to argv[2] : 首先,将foo复制到argv[2]

"foo\0foo\0az\0quux\0"

and then barbaz to argv[1] 然后将barbaz转到argv[1]

"barbaz\0\0az\0quux\0"

You see, this is completely messed up. 您看,这完全搞砸了。

This is why your method doesn't work without also adjusting the pointers in the argv -vector. 这就是为什么如果不同时调整argv -vector中的指针就无法使用方法的原因。

Your second proposed method should indeed work flawlessly. 您建议的第二种方法确实应该可以完美地工作。

In addition to @Ctx's answer regarding the layout of *argv[] you have an additional problem with your array index: 除了@Ctx关于*argv[]的布局的答案之外,您的数组索引还有另一个问题:

Wrong index: 索引错误:

        {
            strcpy(temp, argv[position + 1]);  // You store from index +1
            strcpy(argv[position], argv[position + 1]);
            strcpy(argv[position + 1], temp);  // Restore into same index.
        }

As you store and restore the same index, the copying to temp does not have any effect here. 当您存储和恢复相同的索引时,复制到temp在这里没有任何作用。 You end up with two copies of the same string. 您最终得到相同字符串的两个副本。

Correct index: 正确的索引:

        if (strcmp(*(argv + position), *(argv +position + 1)) > 0 )
        {
            i = *(argv + position);  // <<== Store from pos
            *(argv + position) = *(argv + (position + 1));
            *(argv + position + 1) = i; // <<== Restore into pos+1
            swap = 1;
        }

You store position and restore into position + 1 . 您存储position并恢复到position + 1 This is how swapping works. 这就是交换的工作方式。

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

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