Course List http://www.c-jump.com/bcc/
|
|
|
|
|
|
|
|
How could this be pronounced in plain English?
int* ptr;
The pointer declaration int* ptr; can be vocalized backwards:
ptr; "ptr is..."
* "...a pointer to..."
int "...an integer."
int main( ) { int x = 1; // x is an integer int y = 2; // y is another integer int* ptr; // ptr is a pointer to integer ptr = &x; // store the address of x in ptr ptr = &y; // store the address of y in ptr return 0; }
The pointers provide indirect access to our variables. For example,
int main( ) { // Local variables: int x = 5; double d = 0.1; // We need pointers to store addresses: int* x_ptr = &x; double* d_ptr = &d; // Use pointers to modify variables: *x_ptr = 10; // same as x = 10; *d_ptr = 0.2; // same as d = 0.2; return 0; }
But what is the asterisk * doing in front of the pointer???
Dereference operator,
*ptr
provides access to the variable pointed by the address:
x = *ptr; // fetch the value at the address specified by ptr *ptr = 5; // store new value at the address specified by ptr
int main( ) { int x = 1; int* ptr; // (1) ptr is a pointer to an integer ptr = &x; // (2) store address of x in ptr *ptr = 100; // (3) store new value at the address pointed by ptr return 0; }
Here, the asterisk * is a type modifier, not a dereference.
Ampersand & operator returns an address of a variable: ptr = &x;
Finally, an asterisk *ptr dereferences the pointer, providing access to the variable in memory:
*ptr = 100;
Dereference operator *ptr is also known as indirection operator.
An address &x tells where the variable is located in memory.
Pointer points to a variable by remembering its address:
ptr = &x;
Pointers are variables and must be declared:
double* d_ptr;
Here, asterisk * identifies d_ptr as a pointer variable.
The phrase "variable pointed to by pointer ptr" is translated into C++ as
*ptr
where * is the dereference operator.
Consider
std::cout << "HELLO";
Literal "HELLO" is stored in memory as a sequence of bytes:
H | E | L | L | O | \0 |
String literals are said to be zero-terminated: the sequence is terminated by null character '\0'.
Programs can easily find the end of the string by looking for '\0'.
String literal returns back the address of the first character in memory:
Address | ||||||
00476FEC: | H | E | L | L | O | \0 |
A pointer can be assigned to keep the address of the character string:
char* phello = "HELLO";
Pointer variable phello stores physical address of the first character in the string, which is 'H' in our example.
#include <iostream> using namespace std; void print( char* message ) { cout << message << '\n'; } int main( ) { char* phello = "Hello"; print( phello ); print( "World" ); return 0; }; /* Program output: Hello World */
Pointers and arrays are closely related. For example,
char* ptr = "hello"; // pointer char arr[] = "hello"; // array
Null character is also included in the array, so sizeof(arr) returns 6:
arr[0] | arr[1] | arr[2] | arr[3] | arr[4] | arr[5] |
H | E | L | L | O | \0 |
Construction of the arrays of pointers is also allowed.
Most frequent use of arrays of pointers is to store character strings of variable lengths:
char* months[] = { "Illegal", "Jan", "Feb", "Mar" };
months[ 0 ] = 00476FEC : | I | l | l | e | g | a | l | \0 |
months[ 1 ] = 00476FF4 : | J | a | n | \0 | ||||
months[ 2 ] = 00476FF8 : | F | e | b | \0 | ||||
months[ 3 ] = 00476FFC : | M | a | r | \0 |
#include <iostream> using namespace std; int main( ) { char* months[] = { "Illegal", "Jan", "Feb", "Mar" }; // Each array element keeps address of a character string: cout << months[ 0 ] << '\n'; cout << months[ 1 ] << '\n'; cout << months[ 2 ] << '\n'; cout << months[ 3 ] << '\n'; return 0; } /* Program output: Illegal Jan Feb Mar */
|
|
swap( ) is a function that swaps values of two integer variables:
void swap( int* px, int* py ) { int temp = *px; *px = *py; *py = temp; } int main( ) { int x = 5; int y = 10; swap( x, y ); // Error: must take address swap( &x, &y ); // Correct return 0; }