Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

1And in Conclusion\dots

2Textbook Reading

K&R: Chapters 5-6

3Additional References

4Exercises

Check your knowledge!

4.1Conceptual Review

Solution to Exercise 1 #

False. The correct way is char array[].

Solution to Exercise 2 #

True. If you want to pass a reference to anything, you should use a pointer.

Solution to Exercise 3 #

As we like to say, “everything is just bits.” A pointer is just a sequence of bits, interpreted as a memory address. An array acts like a pointer to the first element in the allocated memory for that array. However, an array name is not a variable, that is, &arr = arr whereas &ptr != ptr unless some magic happens (what does that mean?).

Solution to Exercise 4 #

It will treat that variable’s underlying bits as if they were a pointer and attempt to access the data there. C will allow you to do almost anything you want, though if you attempt to access an “illegal” memory address, it will segfault for reasons we will learn later in the course. It’s why C is not considered “memory safe”: you can shoot yourself in the foot if you’re not careful. If you free a variable that either has been freed before or was not malloced/calloced/realloced, bad things happen. The behavior is undefined and terminates execution, resulting in an “invalid free” error.

Footnotes
  1. The biggest difference between arrays and pointers comes down to where they are located in memory; this difference leads to the many details you saw in this chapter. See the next chapter for an overarching framework of memory layout that will help you understand the distinction.