Computing.Net > Forums > Programming > Problem with C++

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Problem with C++

Reply to Message Icon

Name: rde
Date: January 15, 2003 at 12:15:13 Pacific
OS: WIN 98 SE
CPU/Ram: AMD Athlon 1800, 256 MB
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Justin
Date: January 15, 2003 at 21:53:10 Pacific
Reply:

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


0

Response Number 2
Name: Ronin1
Date: January 15, 2003 at 22:05:55 Pacific
Reply:

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 variable

string1 = "r";
tempstring = "r";

mystring = joinstr(string1, tempstring);

cout [[ mystring;

HTH


0

Response Number 3
Name: rde
Date: January 17, 2003 at 06:54:17 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Delphi programmers very large c++ calculatio...



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Problem with C++

problem with C www.computing.net/answers/programming/problem-with-c/4471.html

I got a problem with C#'s csc.exe www.computing.net/answers/programming/i-got-a-problem-with-cs-cscexe/11364.html

Problem with newran in C++ www.computing.net/answers/programming/problem-with-newran-in-c/9719.html