strcmp的奇怪行为[C]

时间:2015-10-16 02:26:42

标签: c arrays string debugging strcmp

我做错了吗?我正在尝试将 test msg 与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

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

#child{ height: 100%; overflow: hidden; } 函数返回一个指向decode函数本地数组的指针。

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

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