Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TC++PL 6.6.13 - new operator mystery

Status
Not open for further replies.

JasonUMD

Programmer
Dec 12, 2001
9
JP
Hello. Newbie working self through TC++PL.

Ex: 6.6.3: Write a function cat that concatenates 2 C-style strings. Use new to find store for the result.

My code:
Code:
#include <iostream>

using namespace std;

char* cat(const char* str1, const char* str2){

	char *catstr = new char[(strlen(str1) + strlen(str2) + 1)];
	
	int i = 0;
	
	for(; *str1; i++){
		catstr[i] = *str1++;
	}

	for(;*str2;i++){
		catstr[i] = *str2++;
	}		

	return catstr;

}


int main() {
	char str1[] = &quot;Hello &quot;;
	char str2[] = &quot;world!&quot;;
	char *p = cat(str1, str2);

	cout << p << endl;
	
	return 0;
}

Why does new give an array size of 20?
What am I doing wrong, and is my approach itself correct?
 
Well... the idea is there... it is just hard to read. Does this complile? if so, i dont understand how (unless my order of operations is wrong) If str1 & 2 come in as const you should NOT be able to perform a ++ on them.

i would change the *str1++ and *str2++ to

*(str1+i)


Where do you actually get the 20 from? Are you determining it from the for loop? or strlen + strlen +1?

Matt
 
there is another method....

char *cat(const char *buf1, const char *buf2)
{
char *catstr;

sprintf(buf1 + strlen(buf1), &quot;%s&quot;, buf2);

// or sprintf(catstr, &quot;%s%s&quot;, buf1, buf2);

catstr = buf1;

return catstr;

} Rob
&quot;Programming is like art...It makes me feel like chopping my ear off.&quot;

- Currently down
 
Hey, let me know if that helped! I like to know these things ;-) Rob
&quot;Programming is like art...It makes me feel like chopping my ear off.&quot;

- Currently down
 
Let's change this line:
Code:
cout << p << endl;
To this:
Code:
cout << c << &quot;  :  &quot; << strlen(c) << endl;

Here is the result:
Code:
Hello world!-²²²²²²²  :  20

The statement (strlen(str1)+strlen(str2)+1) should result in 13 (and it does if I check it on a different line), which would be the correct length.

I don't understand how new is getting 20, where I think it should be getting 13.

Also, regarding ++ on str1 and str2. I am reading that as a pointer to const char, which I think means I can move the pointer (which is what I am doing with the ++), but not change the char array pointed to. Please correct me if I am wrong in this line of thought.

Rob, thank you for the alternate method. I will explore it, but I am primarily interested in correcting my misunderstanding of new, since the next exercise is also failing in a smiliar manner.
 
Rob,

The example you give does not compile.

Code:
C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\MyProjects\Test\test.cpp(30) : error C2664: 'sprintf' : cannot convert parameter 1 from 'const char *' to 'char *'
        Conversion loses qualifiers
C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\MyProjects\Test\test.cpp(34) : error C2440: '=' : cannot convert from 'const char *' to 'char *'
        Conversion loses qualifiers
 
I'm sorry, I was at work, quick example...here this will work.

char *cat(char *Abuf1, char *Abuf2)
{
char *catstr;

sprintf(Abuf1 + strlen(Abuf1), Abuf2);

catstr = Abuf1;

return catstr;

}

Make sure you declare
char *cat(const char *buf1, const char *buf2);
in your local functions.

You're welcome ;)
Rob
&quot;Programming is like art...It makes me feel like chopping my ear off.&quot;

- Currently down
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top