Pointers are variables that contain as their values memory addresses of other variables.
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.
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 identical0 is the convention in C++.
The only integer that can be assigned to a pointer without casting is zero.
The & (address) operator returns 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 constants or to expressions that do not return a reference.
The * operator, referred to as the indirection (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 passed. The called function then uses the indirection operator (*) to dereference the pointer and modify the value of the argument in the calling function.
A function receiving an address as an argument must have a pointer as its corresponding parameter.
The const qualifier enables the programmer to inform the compiler that the value of a particular variable cannot be modified through the specified identifier. If an attempt is made to modify a const value, the compiler issues either a warning or an error, depending on the particular compiler.
There are four ways to pass a pointer to a functiona nonconstant pointer to nonconstant data, a nonconstant pointer to constant data, a constant pointer to nonconstant data and a constant pointer to constant data.
The value of the array name is the address of (a pointer to) the array's first element.
To pass a single element of an array by reference using pointers, pass the address of the specific array element.
C++ provides unary operator sizeof to determine the size of an array (or of any other data type, variable or constant) in bytes at compile time.
When applied to the name of an array, the sizeof operator returns the total number of bytes in the array as an integer.
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.
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 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 void * pointer, which is a generic pointer type that can hold pointer values 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.
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.
Pointers that point to arrays can be subscripted exactly as array names can.
In pointer/offset notation, if the pointer points to the first element of the array, 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 using a separate pointer that points to the array.
Arrays may contain 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 argumenta stringinto its first argumenta character array. The programmer must ensure that the target 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 be copied only if the number of characters to be copied is at least one more than the length of the string.
Function strcat appends its second string argumentincluding the terminating null characterto 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 target array used to store the first string is large enough to store both the first string and the second string.
Function strncat is equivalent to strcat, except that a call to 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 with its second string argument character by character. The function returns zero 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 specifies the string to be tokenized as the first argument, and subsequent calls to continue tokenizing the same string specify NULL as the first argument. The function returns a pointer to the current token from 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 stringthe terminating null character is not included in the length of the string.