c - strcmp 的奇怪行为 [C]

标签 c arrays string debugging strcmp

我做错了什么吗?我正在尝试将 testmsg 与 strcmp 进行比较。它们完全相同,但 strcmp 返回负值。

我正在尝试编写速记聊天代码。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


void encode(char* msg, const char * argv[], int argc)  {

   // exit(0);


    const int bmp_header_size = 54;
    int read_code;
    int msg_idx = 0;
    int img_idx = 0;
    int bit_idx = 0;
    char c;
    FILE *img_in = NULL;
    FILE *img_out = NULL;

    if( argc < 3 ){
        printf( "Usage: %s source_bmp output_bmp message.\n", argv[0] );
        exit(1);
    }

    img_in = fopen( argv[1], "rb" );
    if( img_in == NULL ){
        printf( "Could not open the input image file.\n" );
        exit(1);
    }

    img_out = fopen( argv[2], "wb" );
    if( img_out == NULL ){
        printf( "Could not open the output file.\n" );
        exit(1);
    }



    while( ( read_code = fgetc( img_in )) != EOF ){
        c = (char)read_code;

        if( img_idx >= bmp_header_size && msg_idx <= strlen( msg ) ){
            char bit_mask = 1 << bit_idx;

            if( ( msg[msg_idx] & bit_mask) > 0 )
                c |= 1;
            else
                c &= 254;

            bit_idx++;

            if( bit_idx >= 8 ){
                bit_idx = 0;
                msg_idx++;
            }           
        }

        fputc( c, img_out );
        img_idx++;
    }


    fclose(img_in);
    fclose(img_out);

}

char* decode(const char * argv[], int argc) {

    const int bmp_header_size = 54;
    const int max_msg_size = 1000;

    int i;
    int c;
    int img_idx = 0;
    int msg_idx = 0;
    int bit_idx = 0;

    char msg[max_msg_size];
    FILE *img_in = NULL;

    img_in = fopen( argv[2], "rb" );

    if( img_in == NULL ){
        printf( "Could not open the input image file.\n" );
        exit(1);
    }

    for( i=0; i < max_msg_size; i++ )
        msg[i] = 0;

    while( ( c = fgetc( img_in )) != EOF ){
        if( img_idx >= bmp_header_size ){
            char bit_mask = 0;
            if( ( c & 1 )  > 0 )
                bit_mask |= 1 << bit_idx;

            msg[msg_idx] |= bit_mask;

            bit_idx++;

            if( bit_idx >= 8 ){
                if( msg[msg_idx] == '\0' )
                    break;
                bit_idx = 0;
                msg_idx++;
            }           
        }

        img_idx++;
    }

    fclose(img_in);

    char* output = msg;

  //  printf("%s\n",output);

    return output;
}




int main(int argc, const char * argv[]) {

    char message[1000];


  /*  printf("\n Send : ");

    fgets(message, 1000, stdin);
    message[strlen(message) -1] = '\0';


    encode(message, argv, argc);
    decode(argv, argc); */


   while (1) {

       printf("\n Send : ");

        fgets(message, 1000, stdin);


        encode(message, argv, argc);

       char *test;
       char *msg;

       msg = message;
       test = decode(argv, argc);


       long sizem = strlen(msg);
       long sizet = strlen(test);

       printf("size of message : \t %ld\n", sizem);
       printf("size of test : \t %ld\n", sizet);
       printf("Test = \t %s\n", test);
       printf("Message = \t %s\n", msg);


       long debug = strcmp(test, msg);
       printf("strcmp = \t%ld\n", debug);


      /* sizem = strlen(message);
       sizet = strlen(test);

       printf("%ld\n", sizem);
       printf("%ld\n", sizet);
       printf("%s\n", test);
       printf("%s\n", message); */

       // printf("%ld", debeug);

        exit(1);

        while (strcmp(test, message) == 1) {

        }


    }



    return 0;
}

Debug 1 Debug 2

输出:

Send : test
size of message :    5
size of test :   5
Test =   test

Message =    test

strcmp =    -116
Program ended with exit code: 1

感谢您的帮助!

最佳答案

decode 函数返回一个指向 decode 函数本地数组的指针。

局部变量在函数返回时被销毁,因此这是一个悬空指针。在 main 中使用它会导致未定义的行为。您所看到的内容可以通过 strcmp 函数将该数组用于其他目的的内存或任何其他原因来解释。

为了使您的代码正常工作,您必须更仔细地考虑为要存储的解码字符串分配内存的位置。

关于c - strcmp 的奇怪行为 [C],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33161593/

相关文章:

java - java中的二分查找字符串

c - 动态分配整数数组,将它们存储在文件中并在 c 中找到它们的平均值

python - 如何将数组转换为字符,以便我可以从c中的函数返回该字符值。然后,在Python中调用它。

c - 退出函数时无法维护保存在二维字符数组中的字符串

javascript - 计算数组中某个数字出现的次数 javascript

linux - 如何在 Bash 中将空格分隔的文本转换为行

c - Visual Studio 中 C 中的 Weird Stack Error 错误

c - C 中的表达式和 for 循环

javascript - 将数组转换为不同类型(lodash/js)

java - 出现空格后如何区分同一字符串中的不同字符