this function crashes my program, help pls
hi there
this function:
Code:
char* strmerge(const char str1[], char *str2)
{
int x=0, chars=0;
char* str_merged;
while (str1[x]!='\0')
{
str_merged[chars]=str1[x];
x++;
chars++;
}
x=0;
while (str2[x]!='\0')
{
str_merged[chars]=str2[x];
x++;
chars++;
}
str_merged[chars+1]='\0';
return str_merged;
}
crashes my program and i cant seem to figure out why, however if i put a "getch()" anywhere in the function it doesnt crash, for example this:
Code:
char* strmerge(const char str1[], char *str2)
{
int x=0, chars=0;
char* str_merged;
getch();
while (str1[x]!='\0')
{
str_merged[chars]=str1[x];
x++;
chars++;
}
x=0;
while (str2[x]!='\0')
{
str_merged[chars]=str2[x];
x++;
chars++;
}
str_merged[chars+1]='\0';
return str_merged;
}
would not crash.
my main just calls this function, like so:
Code:
int main()
{
strmerge("hi ", "there");
}
i dont see any reason for this function to crash the program, and even more strange is that it gets fixed with a simple randomply placed getch();
Help pls