string concatenation in C with strcat

Hi,

im new to C and experimenting with strings:

1
2
3
4
5
6
7
8
9
10
11
        char *strA = "Hello There";
        char *strB = "Good Bye";
        char *strC[50];
        char *strD[50];
        printf("strA: %s\n",strA);

        strcpy(strC,strB);      
        printf("strB: %s\n",strC);

        strcat(strD,strB);
        printf("strD: %s\n",strD);     



Can somebody please help me with the last printf() call as its not printing out the expected text "Good Bye". Its printing:

1/4.''Good Bye

the 1/4 is a fraction, the . is in the middle center of the character spcae

Thanks

Dave
1/4." could have been the previous contents of that string. stdD is never set to anything before strcat, so it will use the garbage that's in those memory spaces that were allocated.

EDIT: We recommend using C++ strings if you're using C++, although you did say you were using C. Just in case:
1
2
3
4
5
#include <string>
std::string naem;
naem += "text to append";
naem = "text to set string to";
naem = otherstring;


-Albatross
Last edited on
Thanks Albatross, ive decided to learn C before i venture onto C++.

i was just experimenting with what happens when using strcat with an empty destination string.
I still dont understand why the string would show the 1/4.'', as it is empty before the call to strcat().

Any ideas? If this was a consequence of the function being called with an empty destination i would have assumed it would have been included in the documentation. So im stumphed :)

Thanks

Dave
strC and strD are arrays of char pointers instead of arrays of chars (which is probably what you want).

Thanks Albatross, ive decided to learn C before i venture onto C++.

That's a really bad idea, just saying. Because it's likely that you'll keep doing some things the C way (such as this C string mess here) because you're used to it.
Oh... extra detail... strC and strD are of type char**. Iffy things could happen...

Also, as far as I know, C and C++ programs do not delete data in any memory spaces unless told to do so. You never told the program to delete the memory pointed to by strC and strD, though I might just need a big cup of coffee. When I ran this program, though:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
#include <string.h>

int main()
{
	char *strA = "Hello There";
	char *strB = "Good Bye";
	char *strC[50];
	char *strD[50];
	printf("strA: %s\n",strA);
	
	strcpy(strC,strB);      
	printf("strB: %s\n",strC);
	
	strcat(strD,strB);
	printf("strD: %s\n",strD); 
	return 0;
}


Which is an otherwise perfect replica of your own, I got no prefix, which does suggest a memory detail.

-Albatross
Last edited on
You never told the program to delete the memory pointer to by strC and strD, though I might just need a big cup of coffee.

Which is a good thing, because he never allocated any memory either.
You do know that I meant the memory pointED to by strC and strD, right? Because... eh...

-Albatross
ill come back when i get to chapter 16 - memory Allocation!

Thanks for the replies, getting there slowly.

Dave
You do know that I meant the memory pointED to by strC and strD, right? Because... eh...

Nope, but since he's creating strC and strD on the stack, it's all good.
Albatross, just restarted the program im using, codeblocks and the problem is gone. Must have been from one of the many programs i have ran without freeing up memeory allocation.

Thanks again
But you have char strC[50] and char strD[50] in your code now, right? Because if not, these problems will come right back.
yeah, its working fine now thanks. I now have

1
2
        char strC[50];
	char strD[50];
Notice that when you declare strC and strD, you've actually declared arrays of pointers to chars, not chars. I have to assume you simply typed that wrong, because it would never compile the way you've coded it. The proper way to declare them is:

1
2
char strC[50];
char strD[50];


(EDIT: whoops, you corrected this already while I was typing.)


Saying that the destination string is "empty" is a common beginner's misconception. Really, like Albatross said, there's already a bunch of garbage there - that is, there are some random 1's and 0's in those slots of memory which are simply not being used by your computer, and were allocated by your program to your char array. To truly "empty" that block, you have to overwrite those characters yourself. This is often done with:

1
2
3

memset(strDest,0,50); 


...where '50' can be whatever size you need to "empty out", in bytes. It should not exceed the amount you allocated, though. The zero, when used in a string, is the "null terminator". In char form, it is: '\0'. It simply tells C where the character content of a string should actually end in memory, even though the memory allocated to the string may be greater.

When you allocate a string using a "string literal", like this:

 
char *strOne = "Test string";


... a terminating null character is automatically added to the end of the string, but when you allocate it like this:

 
char strTwo[50];


... no terminating null character is appended. strcat() would look for the first '\0' in this string, and it could be anywhere. In your case, there's one right after the junk you're seeing, and that's where the function appends the source string.

It's also entirely possible that there is no zero anywhere, in which case strcat() would go past 50 when looking for the end of the destination string. That would likely result in a runtime error. So always be sure to "empty" the strings yourself before working with them.

Last edited on
Detail: Problem has already been solved.

And that does actually compile. gcc lets it pass.

-Albatross
Topic archived. No new replies allowed.