-2

How can i add Add two int chars together into one char array or string like :

char *s;
int a = 'A';
int b = 'B';
s = a + b;

the terminal givs me :

incompatible integer to pointer conversion assigning to 'char *' from 'int'

  • 1
    What do you want to do? What is the goal you want to accomplish? To create the string `"AB"`? Perhaps you should take some time to [read a couple of good books](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list)? – Some programmer dude Jul 30 '17 at 15:25
  • If you want to create the string `"AB"`, the `+` operator is *not* how you do that in C. – Steve Summit Jul 30 '17 at 15:27
  • 2
    `s = (char[]){a, b, 0};` or `s = (char[3]){a, b};` :-) – alk Jul 30 '17 at 15:28
  • Is it a strict requirement to have `s` be a pointer? As a pointer cannot hold nothing but just an address, there is no room for a `char`-array or a "string" in the code you show. – alk Jul 30 '17 at 15:51
  • I am voting to close this, because there is no indication on what the OP wants - and if there was, this would be a duplicate of about 100 already answered questions. – Antti Haapala -- Слава Україні Jul 30 '17 at 16:00
  • @alk `Is it a strict requirement to have s be a pointer?` Do you think that OP knows what the pointer is? A Good book is needed. – 0___________ Jul 30 '17 at 16:26

1 Answers1

-1

Have a look at sprintf to print the values to a string.

char my_cstring[32] = "";
char a = 'A';
char b = 'B';
sprintf(my_cstring, "%c%c", a, b);
// output: "AB"
Grifplex
  • 192
  • 1
  • 12
  • "*`5`*", "*`9`*"? What? – alk Jul 30 '17 at 15:41
  • 1
    So far the OP hasn't really explained what he's trying to do, but the consensus among answerers so far is that he'd probably want `%c` in your solution, not `%d`. – Steve Summit Jul 30 '17 at 15:42
  • He is probably coming from a language like C# or JavaScript where the `+` operator automatically performs a `.ToString` and concatenates the arguments into a string. – Grifplex Jul 30 '17 at 15:49