Pointers and Strings
- Pointers are variables that contain as their values addresses of other variables.
- The declaration int *ptr; declares ptr to be a pointer to an object 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.
- There are three values that can be used to initialize a pointer: 0, NULL, or an address of an object of the same type. Initializing a pointer to 0 and initializing that same pointer to NULL are identical.
- The only integer that can be assigned to a pointer is 0.
- The & (address) operator returns the address of its operand.
- The operand of the address operator must be a variable name; the address operator cannot be applied to constants, to expressions that do not return a reference, or to variables declared with the storage class register.
- The * operator, referred to as the indirectior or dereferencing operator, returns a synonym, alias, or nickname for the name of the object that its operand points to in memory. This is called dereferencing the pointer.
- When calling a function with an argument that the caller wants the called function to modify, the address of the argument may be passes. The called function then uses the indirection operator (*) to modify the value of the arguments in the calling function.
- A function receiving an address as an argument must include a pointer as its corresponding parameter.
- It is nor necessary to include the names of pointers in function prototypes; it is only necessary to include the pointer types. Pointer names may be included for documentation reasons, but the compiler ignores them.
- The const qualifier enables the programmer to inform the compiler that the value of a particular variable should not be modified.
- If an attempt is made to modify a const value, the compiler catches it and issues either a warning or an error depending on the particular compiler.
- There are four ways to pass a pointer to a function: a non-constant pointer to non-constant data, a non-constant pointer to constant data, a constant pointer to non-constant data, and a constant pointer to constant data.
- Arrays are automatically passed by reference using pointers because the value of the array name is the address of the array.
- To pass a single element of an array call-by-reference using pointers, the address of the specific array element must be passed.
- C++ provides the special unary operator sizeof to determine the size of an array (or of any other data type) in bytes during program compilation.
- When applied to the name of an array, the sizeof operator returns the total number of bytes in the array as an integer.
- Operator sizeof can be applied to any variable name, type, or constant.
- The arithmetic operations that may be performed on pointers are incrementing ( + +) a pointer, decrementing ( - -) a pointer, adding ( + or + =) a pointer and an integer, subtracting ( - or - = ) a pointer and an integer, and subtracting one pointer from another.
- 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 pointed to.
- Pointer arithmetic operations should only be performed on contiguous portions of memory such as an array. All elements of an array are stored contiguously in memory.
- When performing pointer arithmetic on a character array, the results are like regular arithmetic because each character is stored in one byte memory.
- Pointers can be assigned to one another if both pointers are of the same type. Otherwise, a cast must be used. The exception to this is a pointer to void which is a generic pointer type that can hold pointers of any type. Pointers to void can be assigned pointers of other types. A void pointer can be assigned to a pointer of another type only with an explicit type cast.
- A pointer to void may not be dereferenced.
- Pointers can be compared using the equality and relational operators. Pointer comparisons are normally meaningful only if the pointers point to members of the same array.
- Pointers can be subscripted exactly as array names can.
- An array name is equivalent to a pointer to the first element of the array.
- In pointer/offset notation, the offset is the same as an array subscript.
- All subscripted array expressions can be written with a pointer and an offset using either the name of the array as a pointer, or a separate pointer that points to the array.
- An array name is a constant pointer that always points to the same location in memory.
- It is possible to have arrays of pointers.
- A pointer to a function is the address where the code for the function resides.
- Pointers to functions can be passed to functions, returned from functions, stored in arrays, and assigned to other pointers.
- A common use of function pointers is in so called menu-driven systems. The function pointers are used to select which function to call for a particular menu item.
- Function strcpy copies its second argument--a string-- into its first argument--a character array. The programmer must ensure that the array is large enough to store the string and its terminating null character.
- Function strncpy is equivalent to strcpy except that a call to strncpy specifies the number of characters to be copied from the string into the array. The terminating null character will only be copied if the number of characters to be copied is one more than the length of the string.
- Function strcat appends its second string argument--including the terminating null character--to its first string argument. The first character of the second string replaces the null ( ' \ 0 ' ) character of the first string. The programmer must ensure that the array used to store the first string is large enough to store both the first string and the second string.
- Function strncat appends a specified number of characters from the second string to the first string. A terminating null character is appended to the result.
- Function strcmp compares its first string argument to its second string argument character-by-character. The function returns 0 if the strings are equal, a negative value if the first string is less than the second string, and a positive value if the first string is greater than the second string.
- Function strncmp is equivalent to strcmp except that strncmp compares a specified number of characters. If the number of characters in one of the strings is less than the number of characters specified, strncmp compares characters until the null character in the shorter string is encountered.
- A sequence of calls to strtok breaks a string into tokens that are separated by characters contained in a second string argument. The first call contains the string to be tokenized as the first argument, and subsequent calls to continue tokenizing the same string contain NULL as the first argument. A pointer to the current token is returned by each call. If there are no more tokens when strtok is called, NULL is returned.
- Function strlen takes a string as an argument, and returns the number of characters in the string--the terminating null character is not included in the length of the string.