I wrote the following code for string concatnation using pointers in C
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void strCat(char *str1,char *str2);
int main(void)
{
char str1[] = "Rohit";
char str2[] = "Kumar";
strCat(str1,str2);
return 0;
}
void strCat(char *str1,char *str2)
{
int i;
char *start;
start = str1;
printf("%s",str1);
while(*str1++ != '\0')
continue;
while(*str2 != '\0')
*str1++ = *str2++;
*str1 = '\0';
printf("%s\n",str1);
}
Why the ouput is Rohit(null). Please Help!!
Well, first of all
str1isn’t long enough to fit both strings.Here
There are other problems with the code. Perhaps you should try the
string.hstrcatinstead ?