比较C中动态分配的字符串
我正在研究基于另一个函数的返回值动态填充字符阵列的情况。
在某个时候,我想将传入值与静态字符串进行比较并触发动作。
由于某种原因,我无法使strcmp
函数工作。这是我的代码和各自的输出。
代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char* word_generator(int selector)
{
switch(selector)
{
case 0:
return "a";
break;
case 1:
return "jk";
break;
case 2:
return "dfr";
break;
case 3:
return "sbjk";
break;
default:
printf("ERROR: Request out of range!\n");
}
return "";
}
int main () {
for(int i = 0; i < 4; i++)
{
printf("Test string[%d]: %s\n", i, word_generator(i));
printf("\t__STRLEN: %lu\n", strlen(word_generator(i)));
char* input_char_buffer = malloc(strlen(word_generator(i))+1);
strcpy(input_char_buffer, word_generator(i));
printf("\tCurrent buffer (value: %s, length: %zu)\n", input_char_buffer, strlen(input_char_buffer));
char key[] = "dfr";
printf("\tCurrent key (value: %s, length: %zu)\n", key, strlen(key));
/* if(strlen(input_char_buffer) == strlen(key)) */
/* { */
/* printf("\t\tOK\n"); */
/* } */
int ret;
ret = strcmp(input_char_buffer, key);
printf("\t__STRCMP: %d\n", ret);
if(ret == 1)
{
printf("\t\tOK\n");
}
// Clean-up.
free(input_char_buffer);
input_char_buffer = NULL;
}
return 0;
}
输出:
Test string[0]: a
__STRLEN: 1
Current buffer (value: a, length: 1)
Current key (value: dfr, length: 3)
__STRCMP: -3
Test string[1]: jk
__STRLEN: 2
Current buffer (value: jk, length: 2)
Current key (value: dfr, length: 3)
__STRCMP: 6
Test string[2]: dfr
__STRLEN: 3
Current buffer (value: dfr, length: 3)
Current key (value: dfr, length: 3)
__STRCMP: 0
Test string[3]: sbjk
__STRLEN: 4
Current buffer (value: sbjk, length: 4)
Current key (value: dfr, length: 3)
__STRCMP: 15
您可以看到,调试位是正确的值,但是由于某种原因,strcmp
是返回垃圾值。
I'm working on a case where I'm dynamically populating a char array based on the return value of another function.
At some point, I would like to compare the incoming value to a static string and trigger an action.
For some reason, I couldn't get the strcmp
function to work. Here is my code and the respective output.
CODE:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char* word_generator(int selector)
{
switch(selector)
{
case 0:
return "a";
break;
case 1:
return "jk";
break;
case 2:
return "dfr";
break;
case 3:
return "sbjk";
break;
default:
printf("ERROR: Request out of range!\n");
}
return "";
}
int main () {
for(int i = 0; i < 4; i++)
{
printf("Test string[%d]: %s\n", i, word_generator(i));
printf("\t__STRLEN: %lu\n", strlen(word_generator(i)));
char* input_char_buffer = malloc(strlen(word_generator(i))+1);
strcpy(input_char_buffer, word_generator(i));
printf("\tCurrent buffer (value: %s, length: %zu)\n", input_char_buffer, strlen(input_char_buffer));
char key[] = "dfr";
printf("\tCurrent key (value: %s, length: %zu)\n", key, strlen(key));
/* if(strlen(input_char_buffer) == strlen(key)) */
/* { */
/* printf("\t\tOK\n"); */
/* } */
int ret;
ret = strcmp(input_char_buffer, key);
printf("\t__STRCMP: %d\n", ret);
if(ret == 1)
{
printf("\t\tOK\n");
}
// Clean-up.
free(input_char_buffer);
input_char_buffer = NULL;
}
return 0;
}
OUTPUT:
Test string[0]: a
__STRLEN: 1
Current buffer (value: a, length: 1)
Current key (value: dfr, length: 3)
__STRCMP: -3
Test string[1]: jk
__STRLEN: 2
Current buffer (value: jk, length: 2)
Current key (value: dfr, length: 3)
__STRCMP: 6
Test string[2]: dfr
__STRLEN: 3
Current buffer (value: dfr, length: 3)
Current key (value: dfr, length: 3)
__STRCMP: 0
Test string[3]: sbjk
__STRLEN: 4
Current buffer (value: sbjk, length: 4)
Current key (value: dfr, length: 3)
__STRCMP: 15
As you can see, the debug bits are the correct values, but for some reason the strcmp
is returning garbage values.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不用担心,这些数字不是垃圾,而是
strcmp
的工作方式。strcmp
返回0
每当两个字符串匹配时,如果它们不匹配,则返回ASCII表中两个字符串的第一个char
之间的区别。例如:
d
的值来自dfr
是100
在ASCII表中,而s
来自> s
sbjk 是115。因此,115-100 = 15
,这就是您要获得的回报。看来您只想检查两个字符串是否相等。为此,我建议您使用
!strcmp()
而不是strcmp
。这样,如果字符串匹配或0
(如果不是),则将具有1
。您可以稍后检查逻辑操作<代码>!的工作方式以及为什么会发生这种情况。您可以看到更改工作:
Don't worry, these numbers are not trash, it's just how
strcmp
works.strcmp
returns0
whenever both strings match, if they don't, it returns the difference between the firstchar
of both strings in the ASCII table.For instance:
The value of
d
fromdfr
is100
in the ASCII table, whiles
fromsbjk
is 115. Thus,115 - 100 = 15
, and that's the return you're getting.It seems you just want to check if both strings are equal or not. For this, I suggest you to use
!strcmp()
instead ofstrcmp
. This way, you will have1
if strings match or0
if not. You can check later how the logical operand!
works and why this is happening.You can see the change working: