1

I have a file, cr.c.

void main(int argCount, char **args) {
    system("/usr/bin/javac " + args[1]);
}

When I try to compile, it gives me an error:

cr.c: In function ‘main’: cr.c:6:30: error: invalid operands to binary + (have ‘char *’ and ‘char *’)      system("/usr/bin/javac " + args[1]);                               ^

I have tried to fix it by replacing + with ., as I that is one way of joining strings in PHP. It was just a guess.

I do not know why this is happening. Please explain why this is happening, and how to fix it. I would not like for somebody else to judt show me what to type instead, as I want to learn.

Thanks!

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
tylerr147
  • 2,581
  • 2
  • 11
  • 16
  • the `+` operator doesn't work on strings like you would assume or expect. – Ryan May 24 '17 at 17:48
  • I know that. "It was just a guess" – tylerr147 May 24 '17 at 17:49
  • 1
    Research string handling functions such as `strcat` although you cannot concatenate the argument to the literal `"/usr/bin/javac "`. But you can initialise an array with adequate size to hold both strings (and terminator). – Weather Vane May 24 '17 at 17:51

4 Answers4

2

You are probably used to high level languages where you can concatenate strings with the + sign.

In C, you can use strcat:

char buffer[100] = "Hello";
strcat(buffer, " World");
Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
  • What library do I need to to import/include? – tylerr147 May 24 '17 at 17:55
  • Wouldn't strncat be much safer, especially when the string is user input. – hetepeperfan May 24 '17 at 18:16
  • @hetepeperfan: What is the length you pass to `strcnat()`? Suppose you are concatenating onto a variable `char target[64];` — is it correct to write `strncat(target, user_provided_string, sizeof(target));`? If you don't know, you should go look it up. The answer is actually "No — that's wrong, even if `target[0] == '\0';`"! And that's why `strncat()` is not safer; it has a weird interface and is far too easy to get wrong. Don't use `strncat()`! – Jonathan Leffler May 24 '17 at 19:51
  • @jonathanleffler Thanks for the warning! I see that `strncat` uses the size argument for the source and not for the destination. So `strncat` still happily writes beyond the allocated size of destination, thats a proper beginners pitfall! – hetepeperfan May 24 '17 at 20:49
1

The + operator does not concat strings. You can call strcat to concat the two strings together.

Ryan
  • 14,392
  • 8
  • 62
  • 102
  • But you first better ensure that the target string is large enough to hold the new string – FredK May 24 '17 at 18:27
0

to concatenate two strings:

char *buffer = malloc( strlen( stringOne ) + strlen( stringTwo ) + 1 );
if( !buffer )
{ // then malloc failed
    perror( "malloc failed" );
    exit( EXIT_FAILURE );
}

// implied else, malloc successful

strcpy( buffer, stringOne );
strcat( buffer, stringTwo );
user3629249
  • 16,402
  • 1
  • 16
  • 17
-2

Thanks for your question. C is a structured language and here you cant use "."(Member operator) or +(Add operator) as like as c# java Cpp php or javascript. You can simply use the string header file and the predefined functions to make it possible.