Adding a single character to a char array?

Here is my code:

String540 & String540::append(char c){
strncat ((*this).astring, c, 256);
return (*this);
}

I'm trying to add a single character (i.e. 'x') to a char array that is part of my class: String540.

My function call is :

str2.append('x');

str2 is an object of String540 class.
My error is: cannot convert parameter 2 from 'char' to 'const char *
You can only append strings, not individual chars.
1
2
3
4
5
6
7
String540 & String540::append(char c){
  char cc[2];
  cc[0] = c;
  cc[1] = '\0';
  strncat(this->astring, c, 256);
  return *this;
}

str2.append("x");
Good luck!
strncat will work with a single char:

1
2
3
char s[10] = "Tes";
char c = 't';
strncat(s, &c, 1);


You HAVE to pass in a value of 1 for the number of characters to be appended, since obviously a single char cannot be properly terminated. You also have to pass in the address of c, since strncat is expecting a char *
Topic archived. No new replies allowed.