Lecture 23: Arrays (continued)
Overview
Burton Ma explains how C pointer arithmetic can traverse arrays and how array parameters decay to pointers, leaving functions without the array length unless callers pass it explicitly. He also compares ways to return array data, showing why returning a local array is invalid, how dynamic allocation provides longer-lived storage, and why a pointer-to-pointer or static array changes the API's behavior.
Key takeaways
- In C, an array parameter such as int arr[] is adjusted to int *arr, so sizeof inside the function gives pointer size rather than the caller's array size.
- Array-processing functions should receive an explicit length or use a sentinel such as '\0'; explicit lengths avoid a scan but callers must ensure the count does not exceed the array bounds.
- A function must not return a pointer to a local array, because that array's automatic storage duration ends when the function returns.
- Dynamic allocation with malloc(y * sizeof(int)) creates storage that remains usable after the allocating function returns, and the caller also needs the resulting array length.
- A pointer-to-pointer parameter such as int **arr lets a function change the caller's int * variable when the caller passes its address.
- A static local array remains alive for the program but every call reuses the same buffer, so it does not provide independent array results.
Chapters
- Relational comparisons such as p < q indicate element order when both pointers refer to the same array.
- Pointer += n and pointer -= n move by n array elements, not n bytes.
- For p equal to 5, p++ yields the old value 5 before incrementing, while ++p yields the new value 6.
- A forward loop can start p at arr and continue while p < arr + 7, advancing with p++ and processing each element through *p.
- The pointer arr + 7 is the one-past-the-end pointer: it can mark the loop boundary but must not be dereferenced.
- A reverse index loop using size_t and i >= 0 never terminates, because decrementing zero wraps to the largest unsigned value; a signed index avoids that condition.
- For a seven-element array, initialize p to arr + 6 to point at the last element.
- The example tests p against arr, prints the current element with *p, and decrements p to move toward the beginning.
- Dereferencing the pointer directly replaces indexed access such as arr[i].
- A function parameter written as int arr[] is treated as int *arr, so the function receives a pointer to the first element rather than a copied array.
- Because the function receives only a pointer, sizeof arr measures the pointer, not the original array.
- In the example, an 8-byte pointer divided by a 4-byte int produces a misleading calculated length of 2, even when the caller passes 100 elements.
- A common array API passes the element count as a separate parameter, as in a max function that searches a caller-specified number of integers.
- Passing 2 searches only the first two elements, but passing 100 for a five-element array can read unrelated memory because the function cannot verify the actual allocation size.
- C-style strings use the null terminator '\0' as an end marker, but finding the length requires scanning the characters and takes O(n) time.
- C functions cannot return an array directly; returning its first-element pointer also leaves the caller needing the array length.
- A local array such as int arr[100] has automatic storage duration and ceases to be valid when its function returns.
- The example's compiler warns about returning local stack memory, and later writes through the returned pointer produce invalid results.
- The demonstrated function allocates space for 100 integers with malloc(y * sizeof(int)) and returns a pointer to that memory.
- Unlike a local array, dynamically allocated storage remains available after the function returns, allowing the caller to fill and print values 0 through 99.
- The function also writes the returned array length through a size_t * parameter so the caller can learn that the allocation contains 100 elements.
- An alternative function returns the length as its return value and accepts int **arr so it can store the allocated int * in the caller's pointer variable.
- The caller passes &arr; inside the function, *arr = a assigns the newly allocated array pointer to that caller-owned variable.
- The double pointer follows the same rule as an int * output parameter: pass an address when the function must modify a caller-owned value.
- Declaring a function's array static gives it program-long lifetime, so returning its pointer avoids the local-array expiration problem.
- A static array is shared storage, not a fresh result: five calls return access to the same array rather than five independent arrays.
- Dynamic allocation is presented as the general approach when callers need independently returned arrays.
Transcript excerpt
0:01 Okay, good afternoon everybody. So in the last lecture we were talking about arrays um and in particular we were talking about how arrays and pointers are related to one another. Uh so today I want to start by Well, I guess we're going to finish off this lecture and then um perhaps move on to the next lecture if there's time. Um so in the previous uh where where did we end? So we ended um by after talking about comparing pointers. So if you have two pointers P and Q and they're both pointers to the same array into the same array, uh you can use the comparison operators to determine whether or not uh P points at an element that is before or after um the pointer Q. Right? So if P is less than Q, then you
0:49 know that P is pointing at an element uh whose index is less than the index of Q. If P is greater than Q is true, then you know that P is pointing at an element uh whose index is greater than that of the element Q. So the pointer Uh sorry, the comparison operators let you compare the locations of two pointers. Uh and there's a little example here um that we looked at before we ended the class. Uh so it's a little bit uncommon to compare the the the locations of pointers. Um what's more common um is to use pointer arithmetic uh and to manipulate the position of a pointer using the operators plus equals,
1:36 minus equals, plus plus, and minus minus. So if you're not familiar with the plus equals operator, plus equals means uh so P plus equals N means uh add N to the value of P. Right? So this is true whether or not P
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.