1

I need to concatenate a string and a variable to fill another string:

#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;
}

expected result:

text 0, text 1, text 2

Rodrigo Vitor
  • 11
  • 1
  • 6

1 Answers1

2

Ooof! You need to read up on C, pointers and arrays.

Firstly, you need to allocate some space, either using malloc and assign to a pointer or char buffer[200]

Next be aware that you cannot add strings like other languages. You need to either strcat or one of its variants or sprintf(buffer, "text 0 %s, %s",te1, te2);

There's some clues but with no ill intent meant, you really do need to ready up and understand some fundamentals.

LoztInSpace
  • 5,584
  • 1
  • 15
  • 27