#include <iostream> #include <conio.h> // для _getch() #include <clocale> // для погдключения русской локали #include <string.h> #include <stdio.h> //strcat using namespace std; int main(int argc, char *argv[]) { std::setlocale(LC_ALL, "Russian_Russia.1251"); char* slovo1 = " proger "; char* slovo2 = " riger "; //strcpy (slovo1,slovo2); strcat (slovo1, slovo2); printf(slovo1); //printf("прива"); cout << "\n" << slovo1<< "\n"; printf("прива"); std::cout << " Press <Enter> ... "; _getch(); return 0; } 

Unhandled exception in "0x003fd2e9 (msvcr100d.dll)" in "test1.exe": 0xC0000005: Access violation when writing "0x00a47868".

  • does not work - etki
  • one
    And who will allocate memory? Urgent smoke strings and memory allocation. - VladD
  • Why is the tag worth c ++ ?! In C ++, we declare strings through the standard class std::string . He has an operator + , and add it. For pure C, read man asprintf . - 0andriy
  • So C or C ++? In C ++, no char* slovo2 = " riger "; speech can not be, but suddenly used std::cout . What is this mess? - AnT

4 answers 4

Problem in line

 strcat (slovo1, slovo2); 

The strcat function strcat to the string passed in the first parameter a string passed in the second parameter. In this case, strings are arrays of char elements. In C ++, arrays are not dynamic structures, that is, they cannot change their size during program execution. Therefore, there should be enough space in the array (string) passed by the first parameter in strcat to fit both strings. Declaring a string like this

 char* slovo1 = " proger "; 

you create an array of " proger " exactly the length of the " proger " (plus one element under the null terminator). If you want to add two lines, you will have to allocate memory for the result:

 cahr* slovo = new char[strlen(slovo1) + strlen(slovo2) + 1]; strcat(slovo, slovo1); strcat(slovo, slovo2); printf(slovo); 

There are other ways. For example, using the stringstream class:

 stringstream ss; ss << slovo1 << slovo2; printf(ss.str().c_str()); // ну, или cout << ss; 

Or using the string class:

 string slovo1 = " proger "; string slovo2 = " riger "; slovo1 = slovo1 + slovo2; cout << slovo1; 

    You either write in C ++, or in C. You have some kind of porridge. If this is C, then for char * pointers, allocate memory, use printf correctly, or puts instead of it for output. If C ++, make the string concatenation as string and concatenation +. Pay attention to what the compiler says during the compilation process, g ++ issued the following to your program, this is exactly what I wrote above:

     main.cpp:10:22: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings] char* slovo1 = " proger "; ^ main.cpp:11:22: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings] char* slovo2 = " riger "; ^ main.cpp:15:18: warning: format not a string literal and no format arguments [-Wformat-security] printf(slovo1); 

      This is all true, of course, but the real cause of the error is in the attempt to write to the constant domain .

      Usually (as in this case) this area of ​​memory has protection against modification.

      -

      @ timob256 , just write (I will not say that it is necessary to program this way )

        char proger[100 /* достаточно большое число */] = " proger ", *slovo1 = proger, *slovo2 = " riger "; 

      and this error will disappear.

         #include <cstring> #include <iostream> using namespace std; int main() { const char* a = "blabla"; // статический char* b = new char[10]; // динамический memcpy(b, "BLABLABLA", 10); auto la = strlen(a); auto lb = strlen(b); char* c = new char[la + lb + 1]; // динамический memcpy(c, a, la); memcpy(c + la, b, lb + 1); cout << c << endl; delete[] c; delete[] b; return 0; } 

        PS string? no, have not heard...