Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have a problem with one of my programms. The source code looks somewhat like this:
int function()
{
char *string, *tempstring;
.
.
strcat(string, tempstring);
.
.
return ret;
}Before the call of strcat, the strings are:
string = "r"
tempstring = "r"After strcat:
string = "rrrd" ???
tempstring = "r"What happend? I have absolutely no idea. If anyone has an explanation for this,please let me know.
Thanks

I need to see those lines you omitted in order to help... It seems like a simple error to fix though.

Your example is a little vague, so I can only guess. A basic way to return strings is to use a pointer. Note that this does nothing regarding proper memory allocation, so it too may lead to undesired outcomes.
char *retstr(char [], char []); //prototype
..// main
char *str;
str = retstr("r", "r");
cout [[ str;..// end main
char *retstr(char s1[], char s2[]) // def
{
return strcat(s1, s2);
}However, you've got an int return type, so I don't know if you wish to return strings or an int for some sort of operation.
Most string errors with pointers are related to memory usage or improper referencing of variables.
If you post your full source, then someone should be able to answer your question more accurately.
a bit of code
#include [iostream.h]
#include [string.h]char *joinstr(char [], char []);
int main(void)
{
char *mystring;
mystring = joinstr("join", " strings");
cout [[ mystring;return 0;
}char *joinstr(char s1[], char s2[])
{
return strcat(s1,s2);
}or
char *string1, *tempstring;
// note that in modern compilers
// "string" is part of the string class
// so it should not be used as a variablestring1 = "r";
tempstring = "r";mystring = joinstr(string1, tempstring);
cout [[ mystring;
HTH

Thanks a lot for your answers.
Meanwhile, i solved the problem myself. I totally reconstructed the code, so i dont use strcat anymore. I guess it was really a problem with the memory.

![]() |
Delphi programmers
|
very large c++ calculatio...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |