gpt4 book ai didi

const char * str 问题

转载 作者:太空宇宙 更新时间:2023-11-04 01:12:34 25 4
gpt4 key购买 nike

我必须使用 fputs 来打印一些东西,而 fputs 使用“const char *str”来打印。我有 3 个字符串要打印(我不关心它是字符串还是 char[])作为 str。我不知道正确的方法。我使用了 3 个字符串,并将它们添加到一个字符串中,但没有用。我还尝试将字符串转换为 char 但没有任何效果!有什么建议吗?

struct passwd* user_info = getpwuid(getuid()); 
struct utsname uts;
uname(&uts);

我想要我的 char const *str = user_info->pw_name + '@' + uts.nodename

最佳答案

您需要为此创建一个新字符串。我不知道你为什么需要 fputs 限制,但我假设即使你不能/不想使用 fprintf,你仍然有 snprintf 可用。然后你会这样做:

char *new_str;
int new_length;

// Determine how much space we'll need.
new_length = snprintf(NULL, "%s@%s", user_info->pw_name, uts.nodename);
if (new_length < 0) {
// Handle error here.
}
// Need to allocate one more character for the NULL termination.
new_str = malloc(new_length + 1);
// Write new string.
snprintf(new_str, "%s@%s", user_info->pw_name, uts.nodename);

关于const char * str 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8962324/

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