为什么 2 在 Codeblocks 上不小于 10?

Why 2 is not smaller than 10 on Codeblocks?

我刚刚用简单的代码准备了期末考试;当我尝试对字符串进行排序时,我遇到了烦人的错误。为什么 2 在我的 CodeBlocks IDE 上不小于 10 但在真实和 onlinegdb.com 上小于 10?

这是烦人的代码:

#include <string.h>
#include <stdio.h>
#define STR_SIZ 20

int main()
{
    char strArr[][STR_SIZ] = {"abc", "hdas", "sdfasf", "kakldf", "caksl", "casd", "keam", "cznjcx", "mnxzv", "jkalkds"};
    char minStr[STR_SIZ];
    strcpy(minStr, strArr[0]);
    int N = sizeof(strArr)/sizeof(minStr);
   // int N = 10;
    for(int x = 0; x < N-1; x++)
    {
        printf("%d", x);
        strcpy(minStr,strArr[x]);
        int j;
        for(j=1+x; j < 10; j++)
        {
            printf("%4d\n", j);
            int cmp = strcmp(strArr[j], minStr);
            if(cmp < 0)
                strcpy(minStr,strArr[j]);
        }
        char temp[STR_SIZ];
        strcpy(temp,strArr[x]);
        strcpy(strArr[x], minStr);
        strcpy(strArr[j], temp);
    }


    return 0;
}

onlinegdb.com 上的输出:

0   1
   2
   3
   4
   5
   6
   7
   8
   9
1   2
   3
   4
   5
   6
   7
   8
   9
2   3
   4
   5
   6
   7
   8
   9
3   4
   5
   6
   7
   8
   9
4   5
   6
   7
   8
   9
5   6
   7
   8
   9
6   7
   8
   9
7   8
   9
8   9

CodeBlocks 上的输出:

0   1
   2
   3
   4
   5
   6
   7
   8
   9
1   2
   3
   4
   5
   6
   7
   8
   9
2

PS:早上刚用过Codeblock,执行起来还可以。

strArr 有 10 个元素。在循环结束时,调用 strcpy(strArr[j], temp);。这将写入 strArr[10],这是越界的,将覆盖一些未知的内存。之后什么都有可能发生。

将字符串复制到 minStr 时应保存 j 值。

仅供参考,您上面的代码将此打印为您使用 onlinegdb 的最终字符串顺序:

abc                                                                                                                                                                                  
caksl                                                                                                                                                                                
caksl                                                                                                                                                                                
caksl                                                                                                                                                                                
caksl                                                                                                                                                                                
casd                                                                                                                                                                                 
cznjcx                                                                                                                                                                               
cznjcx                                                                                                                                                                               
jkalkds                                                                                                                                                                              
jkalkds   

所以我认为你还有其他问题。

试试这个

#include <string.h>
#include <stdio.h>
#define STR_SIZ 20

int main()
{
    char strArr[][STR_SIZ] = {"abc", "hdas", "sdfasf", "kakldf", "caksl", "casd", "keam", "cznjcx", "mnxzv", "jkalkds"};
    strcpy(minStr, strArr[0]);

    // Calculate the number of elements this way.
    const int N = sizeof(strArr)/sizeof(strArr[0]);

    // int N = 10;
    for(int x = 0; x < N-1; x++)
    {
        printf("%d", x);
        int j;
        for(j=1+x; j < N; j++)  // Use N here too!
        {
            printf("%4d\n", j);
            int cmp = strcmp(strArr[j], strArr[x]);
            if(cmp < 0)
            {
                // Do the swaps only when needed.
                char temp[STR_SIZ];
                strcpy(temp,strArr[x]);
                strcpy(strArr[x], strArr[j]);
                strcpy(strArr[j], temp);
            }
        }
    }
    // Verify result
    for(int x = 0; x < N; x++) printf("%s\n", strArr[x]);

    return 0;
}

我将你的交换移到了你的 if 检查中,并删除了你的 minStr,因为它不需要。请注意我是如何计算 N 大小的。老实说,你很接近,但你需要验证你的输出。