Summary

Section 8.2 Pointer Variable Declarations and Initialization

  • Pointers are variables that contain as their values memory addresses of other variables.

  • The declaration

    
    int* ptr;
    

    declares ptr to be a pointer to a variable of type int and is read, “ptr is a pointer to int.” The * as used here in a declaration indicates that the variable is a pointer.

  • You can initialize a pointer with an address of an object of the same type or with nullptr (p. 342).

  • The only integer that can be assigned to a pointer without casting is 0.

Section 8.3 Pointer Operators

  • The & (address) operator (p. 342) obtains the memory address of its operand.

  • The operand of the address operator must be a variable name (or another lvalue); the address operator cannot be applied to literals or to expressions that result in temporary values (like the results of calculations).

  • The * indirection (or dereferencing) operator (p. 343) returns a synonym for the name of the object that its operand points to in memory. This is called dereferencing the pointer (p. 343).

Section 8.4 Pass-by-Reference with Pointers

  • When calling a function with a variable that the caller wants the called function to modify, the address of the variable may be passed. The called function then uses the indirection operator (*) to dereference the pointer and modify the value of the variable in the calling function.

  • A function receiving an address as an argument must have a pointer as its corresponding parameter.

Section 8.5 Built-In Arrays

  • Built-in arrays—like array objects—are fixed-size data structures.

  • To specify the type of the elements and the number of elements required by a built-in array, use a declaration of the form:

    
    type arrayName[arraySize];
    

    The compiler reserves the appropriate amount of memory. The arraySize must be an integer constant greater than zero.

  • As with array objects, you use the subscript ([]) operator to access the individual elements of a built-in array.

  • The subscript ([]) operator does not provide bounds checking for array objects or built-in arrays.

  • You can initialize the elements of a built-in array using an initializer list. If you provide fewer initializers than the number of built-in array elements, the remaining elements are initialized to 0. If you provide too many initializers, a compilation error occurs.

  • If the built-in array’s size is omitted from a declaration with an initializer list, the compiler sizes the built-in array to the number of elements in the initializer list.

  • The value of a built-in array’s name is implicitly convertible to the address in memory of the built-in array’s first element.

  • To pass a built-in array to a function, simply pass the built-in array’s name. The called function can modify all the elements of a built-in array in the caller—unless the function precedes the corresponding built-in array parameter with const to indicate that the built-in array’s elements should not be modified.

  • Built-in arrays don’t know their own size, so a function that processes a built-in array should have parameters to receive both the built-in array and its size.

  • The compiler does not differentiate between a function that receives a pointer and a function that receives a one-dimensional built-in array. A function must “know” when it’s receiving a built-in array or simply a single variable that’s being passed by reference.

  • The compiler converts a function parameter for a one-dimensional built-in array like const int values[] to the pointer notation const int* values. These forms are interchangeable—for clarity you should use the [] when the function expects a built-in array argument.

  • Function sort (and many other library functions) can also be applied to built-in arrays.

  • C++11’s begin and end functions (from header <iterator>) each receive a built-in array as an argument and return a pointer that can be used with C++ Standard Library functions like sort to represent the range of built-in array elements to process.

  • Built-in arrays cannot be compared to one another using the relational and equality operators.

  • Built-in arrays cannot be assigned to one another.

  • Built-in arrays don’t provide automatic bounds checking.

  • In contemporary C++ code, you should use objects of the more robust array and vector class templates to represent lists and tables of values.

Section 8.6 Using const with Pointers

  • The const qualifier enables you to inform the compiler that the value of a particular variable cannot be modified through the specified identifier.

  • There are four ways to pass a pointer to a function—a nonconstant pointer to nonconstant data (p. 353), a nonconstant pointer to constant data (p. 353), a constant pointer to nonconstant data (p. 354), and a constant pointer to constant data (p. 355).

  • To pass a single built-in array element by reference using pointers, pass the element’s address.

Section 8.7 sizeof Operator

  • sizeof (p. 356) determines the size in bytes of a type, variable or constant at compile time.

  • When applied to a built-in array name, sizeof returns the total number of bytes in the built-in array. When applied to a built-in array parameter, sizeof returns the size of a pointer.

Section 8.8 Pointer Expressions and Pointer Arithmetic

  • C++ enables pointer arithmetic (p. 358)—arithmetic operations that may be performed on pointers.

  • Pointer arithmetic is appropriate only for pointers that point to built-in array elements.

  • The arithmetic operations that may be performed on pointers are incrementing (++) a pointer, decrementing (--) a pointer, adding (+ or +=) an integer to a pointer, subtracting (- or -=) an integer from a pointer and subtracting one pointer from another—this particular operation is appropriate only for two pointers that point to elements of the same built-in array.

  • When an integer is added or subtracted from a pointer, the pointer is incremented or decremented by that integer times the size of the object to which the pointer refers.

  • Pointers of the same type can be assigned to one another. A void* pointer is a generic pointer type that can hold pointer values of any type.

  • The only valid operations on a void* pointer are comparing void* pointers with other pointers, assigning addresses to void* pointers and casting void* pointers to valid pointer types.

  • Pointers can be compared using the equality and relational operators. Comparisons using relational operators are meaningful only if the pointers point to members of the same array.

Section 8.9 Relationship Between Pointers and Built-In Arrays

  • Pointers that point to built-in arrays can be subscripted exactly as built-in array names can.

  • In pointer/offset notation (p. 362), if the pointer points to the first element of a built-in array, the offset is the same as an array subscript.

  • All subscripted array expressions can be written with a pointer and an offset, either using the built-in array’s name as a pointer or using a separate pointer that points to the built-in array.

Section 8.10 Pointer-Based Strings (Optional)

  • A character constant (p. 364) is an integer value represented as a character in single quotes. The value of a character constant is the integer value of the character in the machine’s character set.

  • A string is a series of characters treated as a single unit. A string may include letters, digits and various special characters such as +, -, *, /and $.

  • String literals or string constants (p. 365) are written in double quotation marks.

  • A pointer-based string is a built-in array of chars ending with a null character (''; p. 365), which marks where the string terminates in memory. A string is accessed via a pointer to its first character.

  • The result of sizeof for a string literal is the length of the string including the terminating null character.

  • A string literal may be used as an initializer for a built-in array of chars or a variable of type const char*.

  • You should always declare a pointer to a string literal as const char*.

  • When declaring a built-in array of chars to contain a C string, the built-in array must be large enough to store the C string and its terminating null character.

  • If a string is longer than the built-in array of chars in which it’s to be stored, characters beyond the end of the built-in array will overwrite data in memory following the built-in array, leading to logic errors.

  • You can access individual characters in a string directly with array subscript notation.

  • A string can be read into a built-in array of chars using stream extraction with cin. Characters are read until a whitespace character or end-of-file indicator is encountered. The setw stream manipulator should be used to ensure that the string read into a built-in array of chars does not exceed the size of the built-in array.

  • The cin object provides the member function getline (p. 366) to input an entire line of text into a built-in array of chars. The function takes three arguments—a built-in array of chars in which the line of text will be stored, a length and a delimiter character. The third argument has ' ' as a default value.

  • A built-in array of chars representing a null-terminated string can be output with cout and <<. The characters of the string are output until a terminating null character is encountered.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset