4
#include <stdio.h>
int main ()
{
    char a[] = "My name is";
    char b[] = "kamran";

    printf("%s %s", a+b);

    return(0);

}

I was trying to add two strings but getting error of "Invalid operands to binary"

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
kamran hassan
  • 166
  • 1
  • 1
  • 13
  • It looks like you've omitted the tail of the error message. The whole message is probably something like "Invalid operands to binary +". – John Bollinger May 15 '16 at 16:49
  • You need not add two arrays to print them one after the other. See the answer. If you really want to add them to a third string, the easiest way would be by using functions declared in header. (-: – user3078414 May 15 '16 at 16:49
  • http://stackoverflow.com/questions/8465006/how-to-concatenate-2-strings-in-c – ATul Singh May 15 '16 at 16:49
  • 1
    Add two strings? Do you mean concatenate? You should use `strcat()` then. – Haris May 15 '16 at 16:49
  • @Haris, the OP cannot use `strcat()` to concatenate his strings (as currently written) because there is insufficient space in array `a`. – John Bollinger May 15 '16 at 16:54
  • @JohnBollinger, True. I just wanted to get to know what he exactly wants. I would have given the details after that. – Haris May 15 '16 at 17:01
  • That's not c++, you cant use '+' to concatenate (add) two strings! You must use strcat() – Claudio Cortese May 15 '16 at 17:23

2 Answers2

8

You can concatenate b to a like this:

char a[18] = "My name is "; // a needs to be big enough
char b[] = "kamran";

strcat(a, b);

printf("%s", a);

To use strcat() you need to include string.h.

Viktor Simkó
  • 2,607
  • 16
  • 22
8

In this expression

a+b

the array designators are implicitly converted to pointers to the first characters of the strings. So in fact you are trying to add two pointers of type char *.

From the C Standard (6.3.2.1 Lvalues, arrays, and function designators)

3 Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.

However operator + is not defined for pointers in C and C++.

If you indeed want to add two strings then the result of the operation will be a third string that contains the first two strings.

There are two approaches. Either you declare a third character array large enough to contain the first two strings. Or you need to allocate dynamically memory for the resulted string.

For example

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int main( void )
{
    char a[] = "My name is";
    char b[] = "kamran";
    char c[sizeof( a ) + sizeof( b )];

    strcpy( c, a );
    strcat( c, " " );
    strcat( c, b );

    puts( c );

    char *d = malloc( sizeof( a ) + sizeof( b ) );

    if (  d )
    { 
        strcpy( d, a );
        strcat( d, " " );
        strcat( d, b );

        puts( d );
    }

    free( d );
}

The program output is

My name is kamran
My name is kamran
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335