0
#include <stdio.h>
#include <string.h>
int main()
{
char t1string[] = "test";
printf("First String value is %s\n", t1string);
strcpy(t1string, "lgs");
printf("Modified First String value is %s\n", t1string);

char * t2string = "test";
printf("Second String value is %s\n", t2string);
strcpy(t2string, "lgs");
printf("Modified Second String value is %s\n", t2string);
return 0;
}

Looking at the above pair of strcpy usage, I am trying to understand why the strcpy used in the second instance does not work, or is it something specifically wrong (with either my beginner level understanding or code blocks)? Or is it just telling me I am stupid for not simply doing t2string = "lgs";?

gman
  • 100,619
  • 31
  • 269
  • 393
blessedk
  • 71
  • 7
  • 1
    `char x[] = "abc"` makes an array (which is writable) that is 4 characters long and then copies the literal string into the array (including the string termination character.) `char *x = "abc"` makes a pointer and points it at a literal string (which is not writable) - then you try to strcpy infomration into that non-writable string that your pointer points to. The difference is that you can copy information into an array but you can't copy information into a literal string. – Jerry Jeremiah Dec 14 '20 at 05:45
  • 1
    None of the code you posted is C++. It is entirely C. If it was C++ you would be using std::string and not strcpy or printf. – Zan Lynx Dec 14 '20 at 05:58
  • 1
    Here is a bit more explanation: https://onlinegdb.com/HkAqeK43v – Jerry Jeremiah Dec 14 '20 at 06:05
  • 2
    For the question you are asking it doesn't matter if the tag is C or C++, because either could compile the code you posted. If you wanted to only do things the idiomatic C++ way you wouldn't use pointers or arrays - you would use std::string and std::vector or std::array but that doesn't actually answer your question because your question isn't about best practices for writing idiomatic C++. However, to keep from getting comments about not using C++ specific features you could change the tag to be C – Jerry Jeremiah Dec 14 '20 at 06:09
  • `char * t2string = "test";` this is most definitely illegal in C++ and a C++ compiler should complain about it. – n. m. could be an AI Dec 14 '20 at 06:13
  • It's ridiculous to claim that C++ programmers wouldn't use char*, char[], printf etc. There are huge C++ code bases (webkit, chromium, firefox to name 3) full of experienced C++ programmers using char*, printf, etc.. – gman Dec 14 '20 at 07:13
  • Noted that my tag should be C and not C++. I think my key take away is that the pointer declaration and literal string assignment does not allocate any memory that is writeable, because literal strings are stored in the same section of memory where instructions are stored and not meant to be writeable? Unfortunately while learning at edube.org, this was not highlighted and they simply said it is possible to do strcpy(t2string. "lgs") without first mentioning that memory first has to be pre-allocated in that case. Sorry quite some learning curve coming from other languages, but will get there:) – blessedk Dec 15 '20 at 19:42

1 Answers1

2

Whenever you see a string literal such as "test" in C, that is a const char[5] array which decays for most all purposes into const char *, aka a pointer to a constant character array. The letters 't', 'e', 's', 't' cannot be changed. They are stored in the same area of the program as the program instructions. Those cannot be changed either.

When you declare an array like char s[] = "test"; the constant characters "test" are copied into the array s, exactly as if you had called memcpy(s, "test", 5); From their new location in s they can be modified.

As a backward compatibility support C allows creating char * pointers to const char * arrays. But your program will still crash if you attempt to write into that constant array.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131