you probably didnt get the desired result, and if you did, then try it on another computer, or reboot and try again and you probably wont. take a close look at the output again.
in the main you have a char* 'a' of length 6 (5 + '\0' automatically appended), which you pass to strcat, with b.
in the for loop of strcat you are setting the indices of s ('a' in main) that arent 'yours'. you have declared a variable of length 6, and thats fine and you can do whatever you want with it.
however, on the first true condition of your if statement in the while loop, you will be writting into index 6 (ie 7th character)... but i thought you only 'wanted' 6? the data in the 7th (and other subsequent indices/data) dont 'belong' to you. that data might belong to another program and writing to it will adversely affect that program (and probably lead to a crash).
you have to remember C is a very advanced and powerful language, and the operating system will let you do almost anything you write in your code, even if you shouldnt be able to. Java, for example, wouldnt let you do this.
in your strcat function, (i dont think) theres a way of making _sure_ your not overwriting something you shouldnt be, you just assume that person calling the function is passing s,t, where s is big enough for t also. again, this is C, so this library function assumes this--that the programmer knows what hes doing.
the following is an example of using this code safetly:
Code:
#include <stdio.h>
void stcat(char *s, char *t);
int stlength(char *s);
int main(){
char a[50]="first";
char b[] = "second";
stcat(a,b);
printf("%s\n",a);
getchar();
}
void stcat(char *s, char *t){
int a = stlength(s);
int b = stlength(t);
int c = a +b;
int i;
for(i = 0; i < c; i++ , s++){
if(i >=a){
*s = *t;
*t++;
}
}
}
int stlength(char *s){
int i = 0;
while (*s++ != '\0')
i++;
return i;
}
'a' declared in main is large enough to safely hold the data in 'b' without going out of bounds of whats yours.
a safer way to make sure you dont overwrite something that isnt yours is to dynamically allocate an array, of size 'c' (c + 1 maybe?). you would then change return type of strcat to char* and return your new array.
edit: the following is an example of this
Code:
#include <stdio.h>
char* stcat(char *s, char *t);
int stlength(char *s);
int main(){
char a[]="first";
char b[] = "second";
char* x = stcat(a,b);
printf("%s\n",x);
free(x);
// note that 'strcat' creates a variable dynamically. if you dont use free(), then that space
// will be allocated until you reboot.. this is a memory leak
// always use free() to prevent this, just before your done with the variable
getchar();
}
char* stcat(char *s, char *t){
int a = stlength(s);
int b = stlength(t);
int c = a +b;
int i;
char* newstr = malloc(c * sizeof(char)); // sizeof(char) == 1 so you can use either
// if you dont know dynamic memory then
// you can stick with what you had before, but use it safetly
// its always wise to check if malloc returned null, ie: if (newstr == null)
// this could happen if computer is out of free memory, in which case you should
// not continue the following commands
for (i = 0; i < a; i++)
newstr[i] = s[i];
// note that 'i' still keeps its value here, since its scope is this entire function.
// on this line, i = a
for (; i < c; i++) // dont need the first 'initialization' portion, since i still = a
newstr[i] = t[i-a]; // if the i-a is confusing try and trace it on paper
newstr[c] = '\0'; // always apppend null character
return newstr;
}
int stlength(char *s){
int i = 0;
while (*s++ != '\0')
i++;
return i;
}