Merge pull request #234 from cs341-illinois/fix-char-array-description

Correct description of char arrays vs string literals
This commit is contained in:
Lawrence Angrave
2026-09-13 16:38:15 -05:00
committed by GitHub
2 changed files with 5 additions and 6 deletions
-4
View File
@@ -313,10 +313,6 @@ This is not a sentence and the itemize ends on it. Possibly a leftover note ("Wh
### introc/c_memory_model.tex:75-76 — endianness claim vs. the figure
"We will assume that our machine is big endian. This means that the least significant byte is last." The following figure caption says the four bytes are "filled with 0006", which is the big-endian layout, so the text and figure agree — but the memory-layout example at lines 35-38 and the "zero length array" hack do not depend on endianness at all. Worth a human check that this aside is not confusing students.
### introc/c_memory_model.tex:211 — contradicts the rest of the section
"Unlike pointers, that hold addresses to variables on the heap, or stack, char arrays (string literals) point to read-only memory located in the data section of the program."
The section just spent a page explaining that char arrays are *mutable* copies and that string literals are the read-only ones. Equating "char arrays" with "string literals" here is backwards.
### introc/c_memory_model.tex:66-98 — figures have no alt text
The three \includegraphics figures (memory_model_empty.eps, memory_model_length.eps, memory_model_full.eps) rely on captions only. The captions are descriptive, but there is no alt-text mechanism for screen readers.
+5 -2
View File
@@ -208,8 +208,11 @@ printf("%p : %s\n", ptr, ptr);
strcpy(ptr, "World"); // OK because now ptr is pointing to mutable memory (the array)
\end{lstlisting}
Unlike pointers, that hold addresses to variables on the heap, or stack, char arrays (string literals) point to read-only memory located in the data section of the program. This means that pointers are more flexible than arrays, even though the name of an array is a pointer to its starting address.
Here \keyword{ptr} initially points to a string literal, which lives in read-only memory, so it cannot be modified.
\keyword{ary}, by contrast, is not a pointer at all: it is an array holding its own writable copy of the characters (on the stack, or in static memory if declared globally).
In most expressions the array's name is converted to a pointer to its first element, which is why \keyword{ary} can be passed to \keyword{printf} or \keyword{strcpy}, but the array itself cannot be reassigned.
Pointers are more flexible because they can be pointed at different memory, but whether that memory is writable depends on what they point to.
In a more common case, pointers will point to heap memory in which case the memory referred to by the pointer \textbf{can} be modified.
Commonly, pointers point to heap memory (from \keyword{malloc}), which \textbf{can} be modified.