Вы находитесь на странице: 1из 2

Pointers A pointer is a special type of variable.

Instead of associating a value, a pointer variable stores the memory address of a value in memory. In order to initialize a pointer, an asterisk is used. Pointers still need to be initialized with a data type, since the computer must know how big the memory location that needs to be referenced is. For instance, an int variable takes up two bytes in memory. The pointer shows the location of only the first byte. In order to read the value stored, the computer must know to also take into account the byte directly after it.
int *p_min;

In order to assign p_min the address of an integer variable, the & operator is used.
p_min = &minimum;

p_min now contains the memory address for the value that minimum references. The actual value can also be accessed through the pointer using the dereference operator (*).
*p_min = 15;

The above statement would have the same effect as minimum = 15; One may now ask what the purpose of pointers is. For most programming situations, pointers are not needed. However, the use of pointers makes certain operations easier and more efficient. Largely, pointers are used for dynamic memory allocation. When a variable is initialized in a program, it is considered to be static. The required memory is set aside for that variable at the beginning of the program, which is considered to be a very efficient procedure. Pointers make dynamic memory allocation possible, that is, programmers are able to create and destroy new values in memory as needed in the middle of the program's run time. Although it may be less efficient, it provides the programmer with great flexibility. The syntax for creating a new memory location during run time is
data_type pointer_name = new data_type(value);

For example,
int *num = new int(5);

If there is enough system memory, the above statement sets asides two bytes in memory, assigns a value of five, and sets the address to num. Setting the value is optional, and can be done later in the program by using the pointer with the dereference operator. Dynamic memory allocation is very important when the programmer does not know what kind of variables will be needed by the program, since often times it depends on input given by the user. One pointer variable can be assigned to another pointer variable, so long as they are of the same type.
p_min = p_input;

Creating standard arrays is another major use of pointers. An array is a group of items that appear consecutively in memory. All standard array items must be of the same type. The following syntax is used to define an array in C/C++.
data_type name[number of items] = {initial values};

For instance,
int nums[3] = {5,7,8};

The above defines an array of three numbers - five, seven, and eight. Like any other variables, arrays do not need to be defined at the time they are initialized. If you are initializing an array of complex structures that reference many values, defining them in the initialization statement is often impossible (this will be discussed in a later lesson). In order to reference a specific item in an array, the [] subscript operator is used. Note, the first item is represented by 0. Therefore, the highest number that can be used is n-1, where n is the amount of items in the array.
nums[0] = 20; //now the array contains {20,7,8}

Although an asterisk isn't used to initialize an array, nums is really a pointer to the first item in the array. The subscript operator tells the compiler how many memory spaces away is the item you wish to modify. This is the reason for which arrays can contain items of only one type. If the items were of different types, the computer would not know how many bytes must be offset in order to modify the value you wish. For instance, nums[2], would tell the compiler that four bytes must be offset from the initial position in order to read the value you wish (the first byte for this value is the 5th from the initial one). A good example for the use of arrays is to create a string variable. C/C++ has built in support for single character variables, but not entire strings. An array of characters can be initialized, and used to represent a string. Some popular languages such as Visual Basic do not have support for pointers. When programming with such languages, alternative algorithms are used to perform the required tasks. However, alternative methods are usually less flexible.

Вам также может понравиться