组合字符数组&;C中的一个常量字符指针,指向单个常量字符*

组合字符数组&;C中的一个常量字符指针,指向单个常量字符*,c,arrays,pointers,char,C,Arrays,Pointers,Char,我有以下代码,其中 s[]-生成一个字符数组并 longStr-是一个cons字符*。 我想将这2个字符组合成一个常量字符*,这样就应该先添加s,然后再添加longStr。 如下所示: const char* combinedStr = ADD s[] and then longStr; longStr的大小可以不断变化。因此,静态分配combinedStr不会很好地利用内存。 有没有一种方法可以在不为combinedStr静态分配大小的情况下(也不使用VLA)动态地分配它 代码 vo

我有以下代码,其中

s[]-生成一个字符数组并

longStr-是一个cons字符*。 我想将这2个字符组合成一个常量字符*,这样就应该先添加s,然后再添加longStr。 如下所示:

    const char* combinedStr = ADD s[] and then longStr;
longStr的大小可以不断变化。因此,静态分配combinedStr不会很好地利用内存。 有没有一种方法可以在不为combinedStr静态分配大小的情况下(也不使用VLA)动态地分配它

代码

void concatenate(const char* longStr)
{
    time_t t = time(NULL);
    struct tm timeinfo;
    localtime_s(&timeinfo, &t);
    char s[100];
    strftime(s, sizeof(s), "%c", &timeinfo);

   //NOW I WANT TO Combine "s[]" & longStr in such a way that s should be added 1st followed by longStr.

    const char* combinedStr = ADD s[] and then longStr;


}

您可以使用
malloc
strcpy
strcat

比如:

#include <stdio.h>
#include <time.h>

void concatenate(const char* longStr)
{
    time_t t = time(NULL);
    struct tm timeinfo;
    localtime_r(&t, &timeinfo);
    char s[100];
    strftime(s, sizeof(s), "%c", &timeinfo);

    // Allocate memory and put the string together
    const char* p = malloc(strlen(s) + strlen(longStr) + 1); // note: Add 1 for the string termination
    strcpy(p, s);
    strcat(p, longStr);

    printf("%s\n", p);

    free(p);
}

int main(void) {
    char* p = "Hello world";
    concatenate(p);
    return 0;
}
#包括
#包括
无效连接(常量字符*longStr)
{
时间t=时间(空);
struct tm timeinfo;
本地时间(r&t和&timeinfo);
chars[100];
strftime、sizeof、“%c”和timeinfo;
//分配内存并将字符串放在一起
const char*p=malloc(strlen(s)+strlen(longStr)+1);//注意:为字符串终止添加1
strcpy(p,s);
strcat(p,longtr);
printf(“%s\n”,p);
自由基(p);
}
内部主(空){
char*p=“你好,世界”;
连接(p);
返回0;
}

strcpy和strcat,如4386427的回答中所述。您必须小心缓冲区溢出(如该答案所示)

其他选项有sprintf和(如果不使用太旧的编译器)snprintf

void concatenate(const char* longStr)
{
    time_t t = time(NULL);
    struct tm timeinfo;
    localtime_s(&timeinfo, &t);
    char s[100];
    strftime(s, sizeof(s), "%c", &timeinfo);

    //NOW I WANT TO Combine "s[]" & longStr in such a way that s should be added 1st followed by longStr.
    // calculate the size:
    //  either this:
    int buflen = snprintf(NULL, 0, "%s%s",s, longstr) + 1;
    // or this:
    int buflen = strlen(s) + strlen(longstr) + 1;

    // allocate:
    const char* combinedStr = malloc(buflen);

    // then either this:
    snprintf(combinedStr, buflen, "%s%s", s, longstr);
    // or this:
    sprintf(combinedStr,  "%s%s", s, longstr);  

    // do what you need to with combinedStr

    free(combinedStr); 

}

记住在使用combinedStr时释放内存。如果您将其从函数中传递出来并在函数中使用,则需要在完成后将其释放到函数之外。

strcat()
snprintf()
是您的朋友。但是要小心
常量
strcat
?请注意,您需要为组合字符串分配更多的存储空间。您需要知道
longStr
的长度(记住空字节)。你应该展示一下你要用组合字符串做什么。如果您只是要打印它,那么您不需要显式地创建它。由于您不将其作为函数值或通过参数列表返回,这意味着您还必须记住释放组合字符串,除非您使用VLA(可变长度数组)。@JonathanLeffler:longStr的大小可能会不断变化。如果不为combinedStr静态分配大小,就可以在不使用VLA的情况下动态分配大小。获取未处理的异常。执行strcpy时读取位置错误。@codeLover-我更正了对
localtime\r
的调用,以便您可以重试