Adding a char to a char pointer in C / C ++

-1

I need to add char to a char pointer. For example: I have a pointer of char named name, which receives "log", after the process, I want it to be "log1".

I tried to implement this, but it did not work.

 bool trocarNome(char *nome){
     nome +='1';
     cout<<nome;
  }
    
asked by anonymous 27.04.2016 / 19:33

2 answers

4

In C, you can use the function strncat, it would look like this:

#include <string.h>
...
bool trocarNome(char *nome){
    strncat(nome, "1", 1);
}
    
27.04.2016 / 19:41
3

Julius's answer works and is technically correct. But the code is written in C ++. And the one recommended in this language is to use string type whenever possible. If there is no reason to avoid it, it would be better to do this:

bool trocarNome(string nome) {
    nome += '1';
    cout << nome;
    return true;
}

See running on ideone .

    
28.04.2016 / 02:11