3

I know that there are two ways to use 'strings' in C--

char foo[NUMBER] = "bar";

char *foo = "bar";

I also know that, due to the way C stores the former, they are immutable--I can't reassign a new string to the array. So to have a mutable string var I have to use a pointer. But how does C store/know the length of the the new string I'm assigning--

*foo = "hello";

This string is longer than the first string, so if C had seen that it only needed 3 bytes to the store 'foo' at the first assignment, and thus allocated that memory, how does it change that, if it has to make foo point to a longer string?

Ben Granger
  • 481
  • 6
  • 19
  • Did you mean `foo = "hello";`? (without dereferencing `foo`) – jadhachem Oct 16 '15 at 04:38
  • "I also know that..." that statement is false, a char array may be written to – M.M Oct 16 '15 at 04:38
  • yep, forgot semicolons, and @M.M, I thought you weren't allowed to reassign after initialization? – Ben Granger Oct 16 '15 at 04:42
  • @BenGranger variables that are not `const` may be modified at any time, e.g. `int x = 5; x = 6; x = 7;` – M.M Oct 16 '15 at 04:44
  • But what happens if I reassign and the length changes, if C has already allocated x number of bytes in memory to fit "bar" and "bar" only – Ben Granger Oct 16 '15 at 04:47
  • @BenGranger the length can't change. You seem very confused , I'd suggest starting with a book or other tutorial that explains things like variables. [Here are some book ideas](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) – M.M Oct 16 '15 at 04:48
  • Ok thank you very much – Ben Granger Oct 16 '15 at 04:49
  • Try compiling your code. If there are any warnings, this probably means your code is invalid. Read them. What does your compiler say when you try to compile `*foo = "hello";`? You should always use a high warning level. – n. m. could be an AI Oct 16 '15 at 05:55
  • Yea, I have -Wall on, that error is in there, among many others, its tricky coming from C++ where I don't really have to think about strings on a low level – Ben Granger Oct 16 '15 at 06:08
  • If your code produces warnings you don't understand, your question should be *what do these warnings mean and how do I fix them?* If you try to discuss the code from any other perspective, you will arrive at this question sooner or later. Skip the waste of time and cut straight to business. – n. m. could be an AI Oct 16 '15 at 20:14

2 Answers2

2

When you write down double quoted strings in 'C', those are stored into Text Segment (constant data) of the memory.

char foo[NUMBER] = "bar" : copies "bar" into data segment.

char *foo = "bar" : char *foo stored into data memory,foo only points to "bar" stored in text segment.

You can anytime change the location where foo should point by foo = "hello".

Vagish
  • 2,520
  • 19
  • 32
  • Meaning, the array format stores the string/char array in memory, whereas the pointer format only stores the pointer in memory, which can be changed since its a constant size? – Ben Granger Oct 16 '15 at 04:41
  • 1
    @BenGranger: The memory allocation for a program is generaly categorized as : Text Segment (Code and Constants such as strings stored in ROM/HDD and may be moved to RAM for execution) and Data Segment (Stack,Heap,Global Variables,allocated on RAM). Now `char foo[NUMBER]` is a array which may be stored on Stack or Glabal Data memory, taking `NUMBER` bytes. `char *foo` is also stored into data memory taking '4'*** bytes. But the double quoted strings are stored onto Text Segment. Thus compiler can simply generate instructions to modify where `foo` is pointing to at any place in the code. – Vagish Oct 16 '15 at 04:56
  • Neither stack nor heap are included in the common definition of Data Segment. – kaylum Oct 16 '15 at 05:00
  • @kaylum ,you are correct but I am trying to make it less confusing as possible for beginner. – Vagish Oct 16 '15 at 05:02
  • What if `NUMBER` was `3` above? There should be no `NUMBER` in the direct initialization. – David C. Rankin Oct 16 '15 at 05:11
  • `foo = "hello"; `, not `*foo = "hello"; `. – jadhachem Oct 28 '15 at 07:49
0

The string was already stored in your program's constant section because you put it in the source code. Assigning from a string literal doesn't allocate any new memory, it just changes the pointer (which is a constant size) to point at the location where the string already exists.

Random832
  • 37,415
  • 3
  • 44
  • 63
  • So when compiled, every single thing in double quotes, is stored in a different place in memory, and as the program runs, foo is changed to point to each one when I 'assign' a new string to it? – Ben Granger Oct 16 '15 at 04:39