Посібник для початківця про вказівникиAnother translations: into Russian. | Participants
|
- Statistics
- Participants
- Translate into Ukrainian
- Translation result
- Translated in draft, editing and proof-reading required.
If you do not want to register an account, you can sign in with OpenID.
A Beginner's Guide to Pointers | ||
What Are Pointers? | ||
Pointers are basically the same as any other variable. However, what is different about them is that instead of containing actual data, they contain a pointer to the memory location where information can be found. This is a very important concept. Many programs and ideas rely on pointers as the basis of their design, linked lists for example. | Грубо кажучи, вказівники - це ті ж самі змінні. Однак, їх відмінність від простих змінних полягає в тому, що замість фактичних даних вказівник містить адресу комірки пам’яті, де знаходиться інформація. Це дуже важливе поняття. Багато програм та ідей покладаються на вказівник як основу для їх розробки, наприклад, зв'язані списки. | |
Getting Started | ||
How do I define a pointer? Well, the same as any other variable, except you add an asterisk before its name. So, for example, the following code creates two pointers, both of which point to an integer: | Як нам визначити вказівник? Ну, так само як і інші змінні, за вийнятком того, що потрібно поставити зірочку перед його ім'ям. Наприклад, наступних два рядки створюють два вказівники на ціле число: | |
int* pNumberOne; | ||
int* pNumberTwo; | ||
Notice the "p" prefix in front of the two variable names? This is a convention used to indicate that the variable is a pointer. Now, let's make these pointers actually point to something: | Звернули увагу на префікс "p" в обох іменах змінних? Цей прийом використовується для того, щоб показати, що змінна є вказівником. Тепер давайте зробимо так, щоб вказівники вказували на щось: | |
pNumberOne = &some_number; | ||
pNumberTwo = &some_other_number; | ||
The & (ampersand) sign should be read as "the address of" and causes the address in memory of a variable to be returned, instead of the variable itself. So, in this example, pNumberOne is set to equal the address of some_number, so pNumberOne now points to some_number. | Знак амперсанд (&) слід розуміти як "адреса чого- небудь", він означає, що буде одержана саме адреса змінної у пам'яті, а не значення самої змінної. Таким чином, у цьому прикладі, у pNumberOne зберігається адреса змінної some_number , тому pNumberOne тепер вказує на some_number. | |
Now if we want to refer to the address of some_number, we can use pNumberOne. If we want to refer to the value of some_number from pNumberOne, we would have to say *pNumberOne. The * dereferences the pointer and should be read as "the memory location pointed to by," unless in a declaration, as in the line int *pNumber. | Тепер, якщо потрібно звернутися за адресою змінної some_number, можна використовувати pNumberOne. Для такого звертання слід було б написати *pNumberOne. Кажуть, що оператор (*) розіменовує вказівник. Це значить, що він повертає значення в комірці пам'яті, на яку вказує цей вказівник. За винятком самого оголошення вказівника int *pNumber. | |
What We've Learned So Far: An Example | ||
Phew! That's a lot to take in. I'd recommend that if you don't understand any of those concepts, to give it another read through. Pointers are a complex subject and it can take a while to master them. Here is an example that demonstrates the ideas discussed above. It is written in C, without the C++ extensions. | Хух! Це досить важко. Якщо ви не зрозуміли, в чому полягає суть вказівників, рекомендую ще раз уважно перечитати матеріал. Вказівники - це складний матеріал, і може знадобитись доволі багато часу, щоб опанувати його. Ось приклад, що демонструє ідеї, які обговорювалися вище. Він написаний на C, без розширених можливостей C++. | |
#include <stdio.h> | ||
void main() | ||
{ | ||
// declare the variables: | ||
int nNumber; | ||
int *pPointer; | ||
// now, give a value to them: | ||
nNumber = 15; | ||
pPointer = &nNumber; | ||
// print out the value of nNumber: |
© Andrew Peace. License: The Code Project Open License (CPOL)

— Шкода, та в українській мові поки ніхто не скасував правила, згідно з яким іменники в місцевому відмінку мнижини мають закінчення -ах/-ях. — untermensch