problem with strcat

Hello,
I have a problem with strcat and I am not sure why.
An example of what I am trying to do might be the following:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
#include <map>

int main()
{
	const char* anna = "Anna";
	const char* maria ="Maria";
	strcat(anna, maria);
	std::cout << anna << std::endl;
	return 0;
}



I understand that the first argument of strcat has to be a non-const char*. Is it possible to cast it? Or is there any other way to do it?

Thanks in advance.
#include <cstring> for C-style string operations
#include <map> is not needed here.

The memory occupied by "Anna" and "Maria" is read only.

To use strcat, you must have enough storage allocated to hold the result. The only thing you have storage for here is two pointers.

Prefer to use std::string.

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>

int main()
{
    std::string anna("Anna") ;
    std::string maria("Maria") ;
    std::cout << anna + maria << '\n' ;
}
Last edited on
You can use const_cast:
 
strcat( anna, const_cast < char* > ( maria ) );

but it is very unsafe, at least on my computer.
You could copy anna to a non-const char* and then concatenate it with maria:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <cstring>
#include <map>

int main()
{
	const char* anna = "Anna";
	const char* maria ="Maria";
	char* str;
	strcpy(str, anna);
	strcat(str, maria);
	std::cout << str << std::endl;
	return 0;
}
@Fransje That results in undefined behavior on anyone's computer.

BrentSpinor wrote:
You could copy anna to a non-const char* and then concatenate it with maria:


No, you can't. str is pointing to some random place in memory.

What you could do:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <cstring>

int main()
{
     char anna[256] = "Anna" ;
     const char* maria = "Maria" ;

    strcat(anna, maria) ;
    std::cout << anna << '\n' ;
}


I would take all of this bad advice is a good reason to prefer to use std::string. =)
String literals in C++ have type const char[] and are not allowed to be changed even if the implementation will not place them in a read-only memory.
Instead of pointers to string literals you can use character arrays. For example

1
2
char anna[] = "Anna";
char maria[] ="Maria";


But take into account if you want to append array maria to array anna then array anna shall have enough memory (size) to accept maria. As the size of maria is equal to 6 then anna shall have size equal to at least 10. So the definition of anna shall be

1
2
char anna[10] = "Anna";
char maria[] ="Maria";


In this case you can use function std::strcat

std::strcat( anna, maria );
First of all thank you all for your replies.
Secondly, what if anna and maria are supposed to be const char* ?
How could I do it then?
Casting a 'const T *' to 'T *' and the using the resulting pointer has undefined behavior. You really don't want to do that.
The destination string has to be non-const. There's no way around it.

What you could do is define your own concatenation function that returns a pointer to a dynamically allocated string, but if you're going that far you may as well just use std::string.
Hmm...yeah that is the tricky part... In my task I am not allowed to use std::string.
Last edited on
Topic archived. No new replies allowed.