gpt4 book ai didi

c - c 中的 getenv + string 和转换类型

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

我想添加/home/用户名 + "/path.png?5667!http-get:*:image/png:!!!!!"就像在 Java 中一样。但在 C 中

name = malloc(strlen(hm)+strlen("/path.png?5667!http-get:*:image/png:!!!!!") + 2);

#include <stdlib.h>
#include<stdlib.h>
#include<string.h>
char *hm;
char *full;
hm = getenv("HOME");
full = malloc(strlen(hm)+strlen("/path.png?5667!http-get:*:image/png:!!!!!") + 2);
printf("name = %s\n",name);

我期望:/home/username/path.png?5667!http-get:*:image/png:!!!!!"

最佳答案

不要害怕使用 str 实用程序。 man strcpyman strcat。这是我认为你想要的(帖子中不清楚):

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int main()
{
char* home = getenv("HOME");
char* add = "/path.png?5667!http-get:*:image/png:!!!!!";
char* full = malloc(strlen(home) + strlen(add) + 1);
strcpy(full, home);
strcat(full, add);
printf("Full = %s", full);
return 0;
}

关于c - c 中的 getenv + string 和转换类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54235886/

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