-4
#include <stdio.h>

char *strcat_ (char s1[], char s2[]) {
    int x = 0, y = 0;
    while (s1[x] != '\0') {
        x++;
    }
    while (s2[y] != 0) {
        s1[x] = s2 [y];
        x++;
        y++;
    }
    s1[x] = '\0';
    return s1;
}

main() {
    char c;
    c = *strcat_("John ", "Trump");
    printf ("%s", &c);
}

So there's my code, and when I try to run I get this "Bus error: 10".

I'm really new at this, so please bare that in mind.

Thanks for the help.

DiogoJDO
  • 19
  • 6
  • There are already some answers that might help you regarding the bus 10 error. Have a look here http://stackoverflow.com/questions/8716714/bus-error-10-error and here http://stackoverflow.com/questions/212466/what-is-a-bus-error – luQ Mar 05 '16 at 16:20
  • Replace your lines in main() function by : `char * c;` and `c = strcat_("John ", "Trump");` `printf ("%s", c);` – nnunes10 Mar 05 '16 at 16:21
  • What compiler are you using? Turn on compiler warnings and *pay attention to them*. – Jonathon Reinhart Mar 05 '16 at 16:22
  • @JonathonReinhart I'm using GCC and no errors before compiling besides complaining about no type on main. – DiogoJDO Mar 05 '16 at 16:29
  • Always use `gcc -Wall -Werror`. Never anything less. – Jonathon Reinhart Mar 05 '16 at 16:47

1 Answers1

1

There are some problems in these lines -

char c;
c = *strcat_("John ", "Trump");
printf ("%s", &c);

1. Your function return char * not char .

2. While calling function do not apply * operator to it .

3. You tend to modify a constant in your function which causes UB and as well as not enough memory to hold the concatenated part .

    c = *strcat_("John ", "Trump");
                  ^^^^ This is a constant. 

4. In printf don't pass address of variable if you want to print the content.

You can write as follows -

char a[100]="John";            
//char c;
strcat_(a, "Trump")            //let type be void and let char a[] hold complete string
ameyCU
  • 16,489
  • 2
  • 26
  • 41