2

I am just starting to learn C, and have been reading a textbook by K.N. King about the programming language. In his chapter on Strings, he states that character arrays and character pointers are pretty much the same in terms of being passed into functions. However, they are not interchangeable, and one of the reasons behind this was that the characters in a character array can be modified, while a character pointer points to a string literal. However, earlier in his textbook he states that C stores string literals as an array of characters anyway, so would that not make the character pointers and character arrays the same?

Thank you in advance!

Carl S.
  • 39
  • 1
  • 4
  • Similar: https://stackoverflow.com/questions/10186765/char-array-vs-char-pointer-in-c and https://stackoverflow.com/questions/1704407/what-is-the-difference-between-char-s-and-char-s – babon Aug 07 '17 at 05:15
  • string literals are stored in string table(ro area) and will return a **const** pointer, so that you cannot edit the string literal. The pointer just points to the string and trying to modify the string with the pointer is illegal. Though accessing the characters are similar with pointers and arrays. Arrays have storage for its content where as pointers just point. a pointer to a string literal is something like `const char array_name[] = "string literal"` in global array. This is just for a quick reference. Kindly go through the above links mentioned by others! – infinite loop Aug 07 '17 at 06:08
  • 1
    @infiniteloop unfortunately, string literals have type `char *` (without `const`) and modifying them is just *undefined behavior*, so compilers are free to put them in read-only memory, which most compilers do, but they're not required to. –  Aug 07 '17 at 06:12
  • @FelixPalmen, I think they are implicitly casted from const char* to char* (of course this is now deprecated and using an explicit cast is a good practice) when assigned and string literals return `const char*` atleast to my knowledge. Correct me if I'm wrong – infinite loop Aug 07 '17 at 06:37
  • 1
    @infiniteloop actually this is wrong. IMHO, it would make a lot of sense to give string literals the type `const char *`, but it isn't the case. Btw, a *cast* is always *explicit* in terms of the standard, you mean a conversion ... but there was never an implicit conversion removing a `const` qualifier. The C standard just says writing to a string literal is *undefined behavior*. If you know your target system doesn't even *know* read-only memory, you *can* write to string literals (it's still UB of course) –  Aug 07 '17 at 06:55
  • Hmm, that's indeed a valid duplicate. Because this sort of question is very frequent, I was just attempting to write a really complete explanation of the subject, I guess I'll just make it a standalone document then... –  Aug 07 '17 at 07:18
  • Okay! Thank you all for the help, and I'm sorry about the duplicate. I was searching StackOverflow and couldn't find these questions. Thank you! – Carl S. Aug 08 '17 at 06:13

1 Answers1

0

When you are declaring an array of characters like this :

char str[]="My String";

Then this allocates the memory for an array in the stack memory if it is not declared as global. In stack memory you can modify the contains of your array.

But when you declare a pointer to a string and initialize it by assigning to a string like :

char *ptr="My string";

Then it allocates the memory for pointer in stack memory but it holds the address of a string which is in code memory which you cannot modify

But, as you are using a pointer not a constant pointer and it is stored in stack so you can modify that pointer means you can point it to another memory here you are changing the contains of the pointer itself. But, you cannot change the contains of the memory where it is pointing as it is pointing to read only memory.

ams
  • 315
  • 4
  • 17
  • "allocates the memory for an array in the stack memory" --> Maybe. I would not expect that when `str` is a global array. IAC, C does doe not require this stack usage. "in code memory which you cannot modify" --> close. If modification is attempted, it might "work", it might not. It is UB. – chux - Reinstate Monica Aug 07 '17 at 15:12