strcmp 的奇怪行为 [C]
Weird Behaviour of strcmp [C]
我做错了什么吗?我正在尝试将 test 和 msg 与 strcmp 进行比较。它们完全相同,但 strcmp returns 为负值。
我正在尝试编写速记聊天代码。
#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] == '[=10=]' )
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] = '[=10=]';
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;
}
输出:
Send : test
size of message : 5
size of test : 5
Test = test
Message = test
strcmp = -116
Program ended with exit code: 1
感谢您的帮助!
decode
函数返回一个指向 decode
函数本地数组的指针。
局部变量在函数returns时被销毁,所以这是一个悬空指针。在 main
中使用它会导致未定义的行为。您所看到的可以通过 strcmp
函数或任何其他原因将该数组用于其他目的的内存来解释。
为了使您的代码正常工作,您必须更仔细地考虑为要存储的解码字符串分配内存的位置。
我做错了什么吗?我正在尝试将 test 和 msg 与 strcmp 进行比较。它们完全相同,但 strcmp returns 为负值。
我正在尝试编写速记聊天代码。
#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] == '[=10=]' )
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] = '[=10=]';
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;
}
输出:
Send : test
size of message : 5
size of test : 5
Test = test
Message = test
strcmp = -116
Program ended with exit code: 1
感谢您的帮助!
decode
函数返回一个指向 decode
函数本地数组的指针。
局部变量在函数returns时被销毁,所以这是一个悬空指针。在 main
中使用它会导致未定义的行为。您所看到的可以通过 strcmp
函数或任何其他原因将该数组用于其他目的的内存来解释。
为了使您的代码正常工作,您必须更仔细地考虑为要存储的解码字符串分配内存的位置。