Lecture 20: Floating-point; Pointers
Overview
Burton Ma reviews C floating-point types, IEEE 754 special values, math-library functions, and printf formatting before introducing pointers as typed memory addresses. He explains NULL checks, address-of and dereference operators, void-pointer casts, and why pass-by-value prevents a conventional swap function from changing caller variables; passing their addresses lets swap modify the original doubles.
Key takeaways
- NaN comparisons always evaluate false, including NaN == NaN, so C code must use isnan to test for NaN; isinf tests for infinity.
- C math functions such as sqrt and pow are declared in math.h, and the demonstrated gcc build links their library with -lm.
- In printf, %.12f controls digits after the decimal point, while %10f sets a minimum total field width and keeps the default six fractional digits.
- A C pointer is a typed memory address: & obtains an object's address, and * accesses or assigns the object at that address.
- Dereferencing NULL or an invalid pointer has undefined behavior, which may crash or silently corrupt memory rather than produce a predictable failure.
- Because C passes arguments by value, a function that must change a caller's variables, such as swap, needs pointers to those variables.
Chapters
- C provides float, double, and long double; Burton Ma recommends double as the usual choice for course calculations.
- float.h contains constants describing floating-point limits, while math.h provides infinity and NaN facilities.
- IEEE 754 defines infinity for operations such as floating-point 1.0 / 0.0 and NaN for cases such as 0.0 / 0.0.
- NaN is unequal to every value, including itself, so use isnan rather than == to test for it; use isinf to test for infinity.
- math.h supplies floating-point functions including sqrt, pow, fmax, and fabs; integer absolute-value functions are in stdlib.h.
- C does not support Java-style function overloading, so variants such as fabs, fabsf, and fabsl handle double, float, and long double.
- On the demonstrated system, compiling a program that uses math functions requires linking the math library with gcc's -lm option.
- Burton Ma notes that math.h functions are often not available directly in the course notebooks because the math library is not linked there.
- printf uses %f for float and double arguments, with six digits after the decimal point by default.
- A format such as %.12f requests 12 digits after the decimal point; %.2f and %.1f request two and one, with rounding in the final displayed digit.
- A format such as %10f sets a minimum total field width of 10 characters while retaining the default six fractional digits.
- In formats such as %10.2f, the number before the decimal point specifies field width and the number after it specifies fractional precision.
- A pointer stores a memory address; for an object type T, T * is the type of a pointer that can point to a T object.
- In int *ptr, ptr is the variable name and its type means it can store the address of an int; spacing around * is a style choice.
- Uninitialized pointers may contain invalid addresses, so C programmers must ensure each pointer refers to a valid object before using it.
- Pointers to automatic-storage objects become invalid when the declaring block or function ends, making object lifetime essential to pointer safety.
- NULL represents a pointer to no object; checking ptr == NULL or simply if (!ptr) tests whether a pointer is null.
- Use NULL rather than 0 in pointer code when it improves clarity, since 0 can also represent an integer value or false.
- A NULL pointer cannot safely be dereferenced; its useful role is to indicate the absence of an object.
- NULL can mark the end of a partially filled pointer array, signal an error return, or indicate an omitted function callback.
- The unary & operator obtains an object's address: assigning &a to an int * makes the pointer refer to the int variable a.
- A pointer can be reassigned from one compatible object to another without changing either object's value.
- The unary * operator dereferences a pointer to access the object it points to; reading *ptr retrieves that object's value.
- For example, if a is 99 and ptr points to a, assigning *ptr + 1 to b gives b the value 100 while leaving a unchanged.
- A dereferenced pointer is an assignable expression, so assigning -33 to *ptr changes the object ptr points to.
- Dereferencing a NULL pointer has undefined behavior; the demonstrated program crashes, but a crash is not guaranteed.
- An invalid write may corrupt unrelated memory, and a later dereference or print operation can also be where the failure appears.
- A void * can store the address of objects of different types, such as an int, a double, or the first character of a char array.
- In most expressions, an array name such as str converts to the address of its first element, so assigning str to a void * points at the first character.
- The compiler cannot dereference void * directly because it does not know the pointed-to object's type.
- Cast a void * to the appropriate pointer type, such as (int *)p, before dereferencing; void * is one technique for implementing generic data structures in C.
- C passes arguments by value, so a swap function receiving two doubles can swap only its local parameter copies, not the caller's variables.
- The same failed swap behavior applies to Java primitive parameters because their values are also passed into local parameters.
- To modify caller-owned doubles, define swap with two double * parameters and call it with &a and &b.
- Inside the pointer-based swap, dereferencing *x and *y accesses the original objects, allowing a temporary double to swap their values.
Transcript excerpt
0:01 All right, good afternoon everybody. Um so, we've got two notebooks to work through today, or one and a half anyway. Uh the first notebook is the notebook describing floating point types. Most of this should be reviewed to you uh because the floating point types behave the same in C as they do in Java. Um so, in C, you've got three floating point types. You've got float, uh you have double, and you've got long double. Float and double in C are the same as float and double in Java. Uh Java does not have long double as far as I know, although that may have changed by um as far as I know, there is no long double in Java. Um so, in this course, if you're going to use if you need a floating point number, typically you just reach for a double. That's normally the default uh floating point type. Uh and so, double just means double
0:49 precision floating point arithmetic, uh which is the name of the type described by that, the IEEE 754 standard. Uh so, just a quick mention, there is a header file called float.h. There are some constants there that describe the limits of the floating point numbers, um if you ever need them. Uh now, we never actually go into detail in describing exactly how floating point numbers work. Um so, we never describe to you what a mantissa is, or what a signific Well, you might know what a significant is. Um things like that. So, these constants probably don't make a lot of sense to you. Um if you need them for some reason though, they're in float.h. Most of the time, if you're working with doubles, you're probably doing some math. So, if you want access to the
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.