gpt4 book ai didi

c++ - strtok_r函数如何返回值?

转载 作者:行者123 更新时间:2023-11-30 20:24:24 25 4
gpt4 key购买 nike

我正在对“C”代码进行组件测试。我已阅读 strtok_r 函数的功能,但无法获取要在 strncmp' 函数中传递的返回值。我的代码包含 strtok_rstrncmp 函数,如下所示:

typedef struct BufferN {
uint32_t v;
uint32_t m;
} My_Buffer;

char subsystemstr[64] = { '\0' };
My_Buffer buffer;
char *p_system;
char *p_subsystem;

(void) GetString(&buffer, subsystemstr, sizeof(subsystemstr));
p_system = strtok_r (subsystemstr, ":", &p_subsystem);

for (i = 0u; i < 100; i++)
{
if (strncmp(p_system, "all", 64) == 0)
{
/*Some Code Statement*/
}
}

由于数组 subsystemstr 初始化为“\0”,我将借助函数 GetString 修改该数组值,如下所示:

strncpy(subsystemstr, "all:", 64);

当我打印subsystemstr时,我将数组更新为:

["all:", '\0' <repeats 59 times>]

但是当我打印p_system时(strtok_r的返回值)。我正在得到

[0x388870 ""] 

我很困惑它是如何工作的。实际上我想要 p_system = "all" 的值,以便 'strncmp' 函数可以返回 0。请提出建议。

最佳答案

我怀疑你的理解

p p_system

实际上是(打印 p_system 的地址)
在 gdb 中,命令为

p *p_system

或者,使用内置 printf 命令

printf "%s", p_system

或者,使用 C 函数

call printf("%s", p_system)

或者,

call (void)puts(p_system)

或者,如果您不介意也看到一些地址值

x /s p_system

关于c++ - strtok_r函数如何返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33657163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com