Implementing strcat without string.h in C

Hi everyone. This is similar to my strcpy question previously. I tried to code but when I ran the code, a segmentation fault appears. I think this has something to do with my loop for the array but I have problems resolving it. May I have some help please? Thank you. My codes and the original question are below:

Question: Implement the my_strcat(s1,s2) function to append a copy of string s2, including the terminating null character, to the end of string s1. The initial character of s2 overrides the null character at the end of s1. String s1 is returned.

Hi DrClaude. Oh dear. Hmm.. I need to think about my answers to the other questions, but I had:

Code (Text):

while(s1[i] != '\0')
{
count = count + 1;
i++;
}
i = count;

because I wanted to join str2[0] to str1[5]. However, when i reaches 4 for str1, s1[4] will be pointing at str1[4], but I need to replace str1[5], the NULL char with str2[0], thus I put "i = count" after the while loop since count would be 5 at that time.

I am not very sure what are the values for s1 and s2, but when I return s1 from the my_strcat function, type_berry should bear the pointer s1 and thus when I print it out, it should give me gooseberry'\0'. This is my thought process (which I think may not be correct).. :(

because I wanted to join str2[0] to str1[5]. However, when i reaches 4 for str1, s1[4] will be pointing at str1[4], but I need to replace str1[5], the NULL char with str2[0], thus I put "i = count" after the while loop since count would be 5 at that time.

Before the while loop, both count and i are equal to 0. They are both incremented by 1 at each iteration. After the loop, i is replaced by count. You are basically counting the same thing twice.

Staff: Mentor

Without commenting on the validity of your code, there are some shortcuts you can take.

You don't need braces { } when you initialize an array of char. You can do this:

Code (Text):

char str1[40] = "goose";

If the array is of some other type than char, you still need the braces.

Also, you can let the compiler figure out how much space to allocate for an initialized array. IOW, you can do this:

Code (Text):

char str1[] = "goose";

Here the compiler will allocate enough space the characters of the string + a terminating null character. If you're going to be concatenating additional characters onto the string, then it's fine to specify how big the array should be. Since you're not doing anything to the str2 array, you could declare it similar to the above.

You can declare and initialize a pointer in one statement, like so:

Code (Text):

char * s1 = str1;

str1 is the same as &str1[0]. The name of an array evaluates to the address of the first element of the array.

I see. I will note down your tips and tricks and perhaps use them for the final exams! Hopefully, I can save some time by writing 'less' code. Haha. :)

To DrClaude:

I see. But after staring at it for some time, I still can't write a correct code. May I have some hints please?
My teacher went through the answer during class today but he used a combination of string functions (which were coded by him, i.e. not from string.h library) to solve the question. :O

You've already made the necessary corrections.
Your "my_strcat" that you originally posted is basically correct with two exceptions:
1) Get rid of all references to "count". You aren't using that value.
2) You need to terminate the s1 string with s1='\0';

the resulting executable ends with a segmentation fault, that means you didn't test it before posting.

This is the reason why I posted this here... I have vim, so I can compile the codes I write, and for this, I kept getting segmentation fault no matter what I did.

However, after figuring out a little, I think I should remove the 2nd condition in the 2nd while loop, if not, the 2nd while loop will never run, as it will be while(true&&false). I will post my code separately together with my trace.

Does the debugger work for all types of errors? This is because vim will give you the line number at which the error appears and one can look for the line and try to see what has happened. Does that count as a debugger?

Thank you! :)

To .Scott:

Ah yes, I realise my mistake, but I feel that i=count should still remain because I have to concatenate str2[0] to str1[5], which for the latter is the '\0' character.