C for JavaScript Developers. Memory Allocation. Pointers, Arrays, and Strings

char *str = "cat"; str[0] = 'b'; and char str[] = "cat"; str[0] = 'b';.

Answer: remember, when there’s a string literal in the code, the compiler allocates static storage for the array of characters that it represents, and the literal is replaced with a pointer to that memory during compilation.

So when a pointer variable is initialized (char *str = "cat";), the value might be a read-only memory address (it’s up to the compiler where to place the string).

When we try writing to that address (str[0] = 'b';), the process immediately terminates with an error.

When you initialize an array though, the storage for "cat" is allocated on the stack, which is writable during program execution, so it works fine.

Question 3: why does string copying fail?#include <string.

h>int main() { char *str1 = "cat"; char *str2; strcpy(str2, str1); return 0;}Answer: the strcpy function copies the string pointed to by the second parameter into the array pointed to by the first parameter.

str2 was not initialized with a value, and thus contains cruft.

strcpy still happily treats that cruft as an address, and tries to write to it, but that address is likely not in a writable memory region.

Question 4: why can’t we declare an array of unknown size inside a function without initializing it, like so: int arr[];?Answer: because an automatic variable must have a known size when it’s declared.

When the code is compiled, there are no symbolic names for variables, and all values are rather stored and retrieved by offsets from the beginning or end of the stack frame.

If we don’t know a variable’s size, we don’t know offsets for variables following it.

Question 5: why can we use arrays of unknown size as function parameters?Answer: arrays as function parameters are simply converted to pointers, so the size of such a parameter is always the size of a pointer variable (8 bytes in the case of a 64-bit processor architecture).

Question 6: Why can’t an array be used as a function return value?Answer: see the “Arrays” section.

Question 7: What happens if we compare two strings: "cat" == "cat".Is it going to be true or false?Answer: we first need to understand what we’re comparing here.

A string literal is replaced with a pointer, so two pointers are being compared.

Which has nothing to do with lexicographic comparison of strings (like what the equality operator does in JavaScript).

So do the pointers have the same value.The answer is, maybe.

The compiler may choose to allocate memory separately for each string literal in the code, or reuse memory for identical literals.

This behavior is unspecified, so you shouldn’t rely on it being implemented one way or the other.

Basically, never write code where a comparison operator has a string literal as one of its operands, because you have no control over the address where it will be allocated.

And if you need to lexicographically compare two strings, use the strcmp function.

ConclusionHopefully, it’s clearer now why arrays and pointers function the way they do, and what strings really are.

Certain decisions of the language creators have also probably started making sense — the features that you might previously have seen as quirks are actually based on sound principles.

ResourcesThe C standard: http://www.



pdfAMD64 Linux ABI: https://software.



pdfThe Development of the C Language by Dennis Ritchie: https://www.



htmlThe C FAQ (specifically its “Arrays and Pointers” section): http://c-faq.


htmlEli Bendersky’s excellent articles: https://eli.


net/2009/10/21/are-pointers-and-arrays-equivalent-in-c and https://eli.


net/2011/09/06/stack-frame-layout-on-x86-64/The Full SeriesProgram Compilation.

Source vs Header Files(this article) Memory Allocation.

Pointers, Arrays, and Strings.. More details

Leave a Reply