Lecture 22: Arrays (and pointers)
Overview
C arrays are fixed-size, contiguous sequences, and array expressions commonly convert to pointers to their first element. The lecture connects that behavior to array initialization, null-terminated strings, copying with `memcpy`, pointer arithmetic, and the bounds and comparison rules that make pointer use error-prone.
Key takeaways
- C array names convert to pointers to their first elements in most expressions, but pointer assignment does not copy array contents.
- A C string needs a `\0` terminator within its accessible storage; functions such as `printf` with `%s` and routines from `string.h` rely on that convention.
- `memcpy` counts bytes, so copying `n` elements requires a size such as `n * sizeof(element)` and valid source and destination regions of that size.
- Pointer arithmetic scales by the pointed-to type, making `*(p + i)` equivalent to indexed access when the resulting element is within the same array.
- A one-past pointer is valid for array-bound comparisons and loop termination, but dereferencing it is undefined behavior.
- In C, omitted elements in a partially initialized array are zero-initialized, even though the lecture's spoken explanation incorrectly suggests they may be indeterminate.
Chapters
- C arrays store elements of one type in contiguous memory and have a fixed capacity.
- C does not check array indexes at runtime, so out-of-bounds access can produce undefined behavior rather than a clear error.
- Array initializers must fit the declared capacity; in C, elements omitted from a partial initializer are zero-initialized.
- A C string is a `char` array whose text ends at the first `\0` null terminator, not necessarily at the end of the array.
- The literal `"CIS 220"` needs eight array slots: seven characters plus the null terminator.
- A `char` array used as a string must have a terminator; passing an unterminated array to string functions can read beyond its bounds.
- `const` on an array makes its elements read-only after initialization.
- C does not allow assigning one array to another, including copying a string with an expression such as `str1 = "goodbye"`.
- Copying array elements requires a loop or a library routine; the lecture demonstrates a loop copying three integers from `Y` into `X`.
- `memcpy` takes a destination pointer, a source pointer, and a byte count; its `void *` parameters allow it to work with different object types.
- To copy three integers, pass `3 * sizeof(int)`, not simply `3`, because the size argument counts bytes.
- The source parameter is `const` to indicate that `memcpy` does not modify the source memory.
- The source and destination pointers can identify positions inside arrays, not only their first elements; the example copies four doubles from `Y[4]` into `X[2]`.
- The requested byte count must fit within both the source and destination regions.
- `memcpy` does not recognize strings or add their terminator; copying a complete C string requires copying the `\0` as well.
- In most expressions, an array name converts to a pointer to its first element, so a `char *` can point to the first character of a `char` array.
- Assigning `p = str` copies a pointer value; it does not create a second string or copy the array.
- Dereferencing `p` accesses the first character, while printing `p` with `%s` treats it as the start of a null-terminated string.
- Adding one to a pointer advances it by one element of its pointed-to type, not by one byte.
- For a pointer `p` into an array, `*(p + i)` accesses the element `i` positions ahead, corresponding to indexed access.
- The lecture demonstrates moving from a pointer to the fourth array element to later elements, then modifying the last element through pointer arithmetic.
- C permits forming a pointer one position past an array's final element, such as `arr + n` for an array of length `n`.
- A one-past pointer may be used for loop termination and comparison, but it must not be dereferenced.
- Forming or dereferencing pointers outside the array's allowed range can cause undefined behavior and is a source of security bugs.
- Relational comparisons such as `<` and `>` describe element order when both pointers refer into the same array.
- The lecture illustrates moving `Q` relative to `P` and testing whether `P` points before, at, or after `Q`.
- Comparing pointers from separate arrays is not a reliable way to determine their relative order; the compiler may issue a warning.
Transcript excerpt
0:01 Right. Good morning everybody. Uh so today's lecture is about arrays. Um and in particular the relationship of pointers to arrays. So the first part of this lecture is probably review from a lecture a few either last week or yeah probably last week or the week before where I introduced the types uh in the C language. uh and then we'll move on to some new stuff. Um and we'll see how do u how uh pointers and arrays actually have a very close relationship to one another. All right. So very quickly let's review what an array is. So arrays uh in C and Java are very similar but there are important differences. Uh so in in both languages an array is a contiguous sequence of objects all
0:48 having the same type. Right? Again remember what contiguous means. This means that each object of the array is beside the previous element of the array. Uh in both languages, the capacity, so the maximum number of elements you can put into the array, never changes. Right? If you need a bigger or smaller array, you have to make a bigger or smaller array, copy the elements of the original array into the new array. uh in both languages the array uh is characterized by the type of elements that it holds. So the an array of type an array that holds ints the int is part of the array type name. Uh and then you use square brackets in both languages to access the individual elements. The important differences are uh there's no
1:35 zero length array in C. So in C an array always holds at least or the capacity of an array is always at least one. uh whereas you are allowed to have an empty array in Java, right? An array of length
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Burton Ma.