This is from the book: C++ Weekend Crash Course, pg 142. It is designed to concatenate two strings, and put a hyphen in the middle. It compiles; it works, but I don't understand why it works.

The two strings are entered in main() as szString1 and szString2. During execution, they are sent to the void function concatString() where they are operated on under different names -- szTarget and szSource. When control passes back to main(), the cout is szString1.

I do not understand how the variable szString1 got modified by the void function. I thought void functions didn't return anything. (I understand the call; I don't understand the return.)

Thanks for your help!

// Concatenate.cpp : Defines the entry point for the console application.
// this will concat two strings with "-" in the middle

#include "stdafx.h"
#include <iostream>
#include <string.h>  //required for string functions
using namespace std;

//prototype declaration
void concatString(char szTarget[], char szSource[]);

int _tmain(int argc, _TCHAR* argv[])
{
	// read the first string
	char szString1[256];
	cout << "Enter string #1: ";
	cin.getline(szString1, 128);

	// now the second string
	char szString2[128];
	cout << "Enter string #2: ";
	cin.getline(szString2, 128);

	// concatenate a hyphen onto the first string
	concatString(szString1, "-");

	// now add the second string
	concatString(szString1, szString2);

	//and display the result
	cout << "\n" << szString1 << "\n";

	return 0;
}


void concatString(char szTarget[], char szSource[])
{
	// find the end of the first string
	int nTargetIndex = 0;
	while(szTarget[nTargetIndex])
		{nTargetIndex++;}
		

	//tack the second to the end of the first
	int nSourceIndex = 0;
	while(szSource[nSourceIndex])
		{
			szTarget[nTargetIndex] = szSource[nSourceIndex];
			nTargetIndex++;
			nSourceIndex++;
		}

	//tack on the terminating null
	szTarget[nTargetIndex] = '\0';
}

Recommended Answers

All 7 Replies

Ok this application works but only just.

If both buffers were of max size (128+128) the buffer would not be alrge enough to also hold the - and null characters at the end.

However you want how it works so ill explian as best i can. concatString is a somewhat simple method but effective. If you can visualise an array this becomes easy enough to see how it works pictorially. Imagine both arrays look like below
szString1[.........256 spaces.........]
stString2[..128spaces..]

What happens is that it gets up to 128 chars into each string. so you have at maximum
szString1[<..Data1.><..empty.>]
szString2[<..Data2.>]

As you can hopefully see pictorially is that one can easily shift szString2 down and stick it into szString1 like below

szString1[<..Data1.><..data2.>]

This is essentially wat the code is doing. it seeks the end of data in szString1

int nTargetIndex = 0;
while(szTarget[nTargetIndex])
{nTargetIndex++;}

Then at the next available char position it sticks in the hyphen and then starting with the next space in its memory starts copying from szString2 into the space left.
This is like what i did above aligning szString2 to fit into the storage space.

Once it has copied all the characters from szString2 it appends a Null to the end and returns szString1 which is both "strings" added together or concatenated as the author calls it.

Hope this explains ok for you

Thanks for your attention. I understand what the function is doing; what I don't understand is how szString1 ever gets modified by it.

szString is "sent" to the function on lines 25 and 28, whereupon, it becomes szTarget[]. szTarget[] now has the data. How does szString1 ever contain the concatenated string? Nowhere does it say

szString1 =

There is no return statement. This is a void function, which I thought does not return data (to szString1.)

Thanks again.

szString1 is actually a pointer (or address) to the start of that chunk of memory with all the characters of the string (terminated by NULL or 0 or '\0', it's all the same). So when you pass szString1 as szTarget to the function, then szTarget also points to the start of that chunk of memory with all the characters. So when the function executes it changes the content of that chunk of memory to hold the concatenated string. Finally, when you return from the function call, the memory pointed to by szString1 now contains the concatenated string too (because it's the same memory that is addressed by szTarget).

Oh. That makes perfect sense. I understand it now, although it seems that there is probably a more solid way to write it (like making concatString a char function with a "return," rather than a void function).

Thanks to both of you for answering. Onward to the "class" data type, then pointers!

What I don't understand is why a book that purports to teach C++ spends so much effort on what is effectively a C program. Isn't it easier to understand this?

// read the first string
string s1;
cout << "Enter string #1: ";
getline(cin, s1);

// now the second string
string s2;	
cout << "Enter string #2: ";
getline(cin, s2); 

// concatenate a hyphen onto the first string
s1 += "-";

// now add the second string
s1 += s2;

//and display the result
cout << "\n" << s1 << "\n";

I can certainly understand why people might want to learn how to concatenate character arrays one character at a time; but that's really programming in C, not C++. So why call it C++ programming?

>but that's really programming in C, not C++.
No, it's just a part of C++ that should be considered more advanced due to the lower level nature. The real question is why is a book teaching such things so soon? Most likely it's ignoring the more useful parts of C++ to teach the less useful parts, which suggests either an older book or simply a poorly designed one.

>but that's really programming in C, not C++.
No, it's just a part of C++ that should be considered more advanced due to the lower level nature. The real question is why is a book teaching such things so soon? Most likely it's ignoring the more useful parts of C++ to teach the less useful parts, which suggests either an older book or simply a poorly designed one.

It's a floor wax and a dessert topping! :)

And I completely agree with your (rhetorical) question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.