Посібник для початківця про вказівники

Andrew Peace, “A Beginner's Guide to Pointers”, public translation into Ukrainian from English More about this translation.

See also 100 similar translations

Another translations: into Russian. Translate into another language.

Participants

eReS2412 points
ALEXfanat334 points
igorko174 points
And others...
Join Translated.by to translate! If you already have a Translated.by account, please sign in.
If you do not want to register an account, you can sign in with OpenID.
Pages: ← previous Ctrl next next untranslated
1 2 3 4 5 6 7 8 9

A Beginner's Guide to Pointers

Посібник для початківця про вказівники

History of edits (Latest: eReS 2 years, 2 months ago) §

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

What Are Pointers?

Що таке вказівники?

History of edits (Latest: eReS 2 years, 3 months ago) §

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.

Грубо кажучи, вказівники - це ті ж самі змінні. Однак, їх відмінність від простих змінних полягає в тому, що замість фактичних даних вказівник містить адресу комірки пам’яті, де знаходиться інформація. Це дуже важливе поняття. Багато програм та ідей покладаються на вказівник як основу для їх розробки, наприклад, зв'язані списки.

History of edits (Latest: rom-broiler 2 years, 2 months ago) §

Getting Started

Приступаємо до роботи

History of edits (Latest: eReS 2 years, 3 months ago) §

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:

Як нам визначити вказівник? Ну, так само як і інші змінні, за вийнятком того, що потрібно поставити зірочку перед його ім'ям. Наприклад, наступних два рядки створюють два вказівники на ціле число:

History of edits (Latest: rom-broiler 2 years, 2 months ago) §

int* pNumberOne;

int* pNumberOne;

History of edits (Latest: eReS 2 years, 3 months ago) §

int* pNumberTwo;

int* pNumberTwo;

History of edits (Latest: eReS 2 years, 3 months ago) §

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" в обох іменах змінних? Цей прийом використовується для того, щоб показати, що змінна є вказівником. Тепер давайте зробимо так, щоб вказівники вказували на щось:

History of edits (Latest: rom-broiler 2 years, 2 months ago) §

pNumberOne = &some_number;

pNumberOne = &some_number;

History of edits (Latest: eReS 2 years, 3 months ago) §

pNumberTwo = &some_other_number;

pNumberTwo = &some_other_number;

History of edits (Latest: eReS 2 years, 3 months ago) §

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.

History of edits (Latest: rom-broiler 2 years, 2 months ago) §

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.

History of edits (Latest: igorko 2 years ago) §

What We've Learned So Far: An Example

Для закріплення матеріалу: Приклади

History of edits (Latest: eReS 2 years, 3 months ago) §

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++.

History of edits (Latest: igorko 2 years ago) §

#include <stdio.h>

#include <stdio.h>

History of edits (Latest: eReS 2 years, 3 months ago) §

void main()

void main()

History of edits (Latest: eReS 2 years, 3 months ago) §

{

{

History of edits (Latest: eReS 2 years, 3 months ago) §

// declare the variables:

// оголошення змінних:

History of edits (Latest: eReS 2 years, 3 months ago) §

int nNumber;

int nNumber;

History of edits (Latest: eReS 2 years, 3 months ago) §

int *pPointer;

int *pPointer;

History of edits (Latest: eReS 2 years, 3 months ago) §

// now, give a value to them:

/ / задаємо їхні значення:

History of edits (Latest: rom-broiler 2 years, 2 months ago) §

nNumber = 15;

nNumber = 15;

History of edits (Latest: eReS 2 years, 3 months ago) §

pPointer = &nNumber;

pPointer = &nNumber;

History of edits (Latest: eReS 2 years, 3 months ago) §

// print out the value of nNumber:

/ / вивід значення nNumber:

History of edits (Latest: rom-broiler 2 years, 2 months ago) §
Pages: ← previous Ctrl next next untranslated
1 2 3 4 5 6 7 8 9

© Andrew Peace. License: The Code Project Open License (CPOL)