I decided to go back to the SI and read the K & R book, and here I stumbled again on the topic of pointers, logically everything seems to be clear, but there are some problems with char * and const char * .
Let me give the code and show that all of these are not clear to me!
Example 1:
char *pstr = "Hello, world"; *pstr = 'D'; /// При запуске приложения на этом месте вылетит сбой не понятно! printf(pstr); /// Ну и конечно ничего не выведет, измененную строку я не получу, ой точнее массив из символов Example 2:
const char *pstr = "Hello, world"; *pstr = 'D'; /// Сбоя не будет и программа вообще не скомпилируется т.к компилятор сообщит что мы не можем изменять наш константный объект printf(pstr); /// До сюда дело не дойдет Example 3:
char str[] = "Hello, world"; char *pstr = str; *pstr = 'D'; /// Все ок мы успешно поменяем наш первый элемент массива printf(pstr); /// Мы получим это: "Dello, world" Let us draw a line over all these examples: Why in the first example can I not change each literal separately? (I did not declare an object as constant as in the second example)
PS The third example cited for the overall picture to show that everything works if you enter an additional parameter