【问题标题】:How concatenate string and variable to a string in C [duplicate]如何将字符串和变量连接到C中的字符串[重复]
【发布时间】:2019-03-03 23:01:12
【问题描述】:

我需要连接一个字符串和一个变量来填充另一个字符串:

#include <stdio.h>

int main(){

    char *te1;
    char *te2;
    char *tabela[2];

    te1 = "text 1";
    te2 = "text 2";
    tabela[0] = "text 0, " + te1 + ", " + te2;
    printf("%s\n", tabela[0]);
    return 0;
}

预期结果:

文字 0、文字 1、文字 2

【问题讨论】:

    标签: c arrays string concatenation


    【解决方案1】:

    哎呀!您需要阅读 C、指针和数组。

    首先,您需要分配一些空间,使用malloc 并分配给指针或char buffer[200]

    接下来请注意,您不能像其他语言一样添加字符串。您需要strcat 或其变体之一或sprintf(buffer, "text 0 %s, %s",te1, te2);

    有一些线索,但并非恶意,您确实需要做好准备并了解一些基础知识。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-14
      • 1970-01-01
      • 1970-01-01
      • 2020-08-16
      • 2019-12-15
      • 2014-07-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多