Computing.Net > Forums > Programming > Problem with strings in 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 strings in c++

Reply to Message Icon

Name: rde
Date: April 16, 2003 at 15:33:03 Pacific
OS: WIN98 SE
CPU/Ram: AMD Athlon 1800, 256 MB
Comment:

hi

I have a quite strange problem in one of my programs. I'd like to strcat(string1,"test") two strings, but after that, string1 hasnt changed at all !?! How can this be?

Here is some more code:

void func(void)
{
char *string1;
string1 = "";
.
.
.
strcpy(string1,func2()) //this one works perfectly well (func2 is a function which returns a string)
strcat(string1, "D") //this one doesnt't work. The "D" isnt added to string1
.
.
.
}

func is called many times by main. The problem occurs only after the fith or sixth call. The first few times, everything works fine. Could it be a problem with memory allocation or something like that? I really have no clue and I'd be grateful for every hint.
Hope I posted enough information; if not please tell me.

thanks
rde




Sponsored Link
Ads by Google

Response Number 1
Name: Trip
Date: April 16, 2003 at 20:04:46 Pacific
Reply:

You are not allocating memory for the string in question. You can either
char string[10];
or
char *string1;
string1 = new char[10];

say string1 already contains 9 characters (the trailing \0 is the tenth character) and you want to cat another string, you would have to:
char *string2;
string2 = new char[strlen(string)+strlen(func())];
strcpy(string2,string1);
strcat(string2,func());


0

Response Number 2
Name: Trip
Date: April 16, 2003 at 20:06:32 Pacific
Reply:

Of course I made a mistake, the line should read:

string2 = new char[strlen(string1) + strlen(func() + 1)];


0

Response Number 3
Name: Trip
Date: April 16, 2003 at 20:07:49 Pacific
Reply:

Of course I made another mistake, the line should read:

string2 = new char[strlen(string1) + strlen(func()) +1];

It's late, I'm tired, give me a break.


0

Response Number 4
Name: Trip
Date: April 16, 2003 at 20:14:22 Pacific
Reply:

You know what, forget char strings. Just use strings, as in:

#include string

string s1 = "Whatever";
string s2 = "Other stuff";
string s3 = string1 + string2;
// s3 is now "WhateverOther stuff"

strings are so much easier to use than messing around with char arrays, except for tokenizing.


0

Response Number 5
Name: rde
Date: April 19, 2003 at 19:56:01 Pacific
Reply:

Thanks a lot for your hints.
Im tired too right now so I will try it later. Hope it works.

rde


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon

Perl STDIN programming help in c ple...



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 strings in c++

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

Problem with strings in structs www.computing.net/answers/programming/problem-with-strings-in-structs/6284.html

String in C www.computing.net/answers/programming/string-in-c/9450.html