• Название:

    Лекция 3 Массивы, указатели и память (с заметками)


  • Размер: 0.3 Мб
  • Формат: PDF
  • или
  • Сообщить о нарушении / Abuse

    Осталось ждать: 10 сек.

Установите безопасный браузер



  • Название: Основы программирования - Лекция № 3, 17 марта 2016 г.
  • Автор: Владимир Парфиненко

Предпросмотр документа



фӐӌӍҺӝ ӎӏӍһӏҸӋӋӄӏӍҺҸӌӄӨ
сҿӈӗӄӨ ॏ ߟࡈ ߝߣ ӋҸӏӑҸ ߞߜߝߢ һࡏ


http://xkcd.ru/371





тҸӐӐӄҺӝ






фҾӌӍӋҿӏӌӝҿ ӋҸӐӐӄҺӝ

тӍӑӄҺҸӗӄӨࡇ ҾӊӨ ӃҸҾҸӌӄӨ ӈҺҸҾӏҸӑӌӍһӍ ӒӏҸҺӌҿӌӄӨ ӌӒӂӌӍ ߟ
ӎҿӏҿӋҿӌӌӝҿࡇ ŏࡈ ųࡈ żࡏ Я ӖӑӍ ҾҿӊҸӑӛ Ӑ ӒӏҸҺӌҿӌӄҿӋ ߝߜࡷӍӅ Ӑӑҿӎҿӌӄࡐ
хӏҸҺӄӊӛӌӍࡈ ӌӒӂҿӌ ӋҸӐӐӄҺࡋ

тҸӐӐӄҺ ࡴ ӒӎӍӏӨҾӍӖҿӌӌӝӅ ӌҸҹӍӏ ӢӊҿӋҿӌӑӍҺ ӍҾӌӍһӍ ӑӄӎҸࡏ

аҿӃ ӄӌӄӗӄҸӊӄӃҸӗӄӄ ӋҸӐӐӄҺࡈ ӍҹӜӨҺӊҿӌӌӝӅ Һ ӔӒӌӈӗӄӄࡈ ӐӍҾҿӏӂӄӑ
ӋӒӐӍӏࡏ

фҹӜӨҺӊҿӌӄҿ ӋҸӐӐӄҺҸ arrࡈ ӐӍӐӑӍӨәҿһӍ ӄӃ N ӢӊҿӋҿӌӑӍҺ ӎӏӍӄӃҺӍӊӛӌӍһӍ ӑӄӎҸ Tࡇ

љӊҿӋҿӌӑӝࡈ ҾӊӨ ӈӍӑӍӏӝӕ ӌҿӑ ӨҺӌӍ ӃҸҾҸӌӌӍһӍ ӃӌҸӖҿӌӄӨࡈ
ӄӌӄӗӄҸӊӄӃӄӏӒӧӑӐӨ ӌӒӊӨӋӄࡏ

T arr[N];

һҾҿ N ࡴ ӈӍӌӐӑҸӌӑҸ ҺӏҿӋҿӌӄ ӈӍӋӎӄӊӨӗӄӄࡏ

цҸӃӋҿӏ ӋҸӐӐӄҺҸ ӋӍӂҿӑ ҹӝӑӛ ҺӝӖӄӐӊҿӌ ҸҺӑӍӋҸӑӄӖҿӐӈӄࡈ ӖӑӍ ӋӍӂҿӑ
ҹӝӑӛ ҾӍҺӍӊӛӌӍ ӒҾӍҹӌӝӋࡏ

хӏӄӋҿӏ ӍҹӜӨҺӊҿӌӄӨ ӄ ӄӌӄӗӄҸӊӄӃҸӗӄӄ ӋҸӐӐӄҺҸࡇ
int xs[4] = {20, 10}; // {20, 10, 0, 0}
int ys[] = {20, 10}; // {20, 10}

ߟ





фҾӌӍӋҿӏӌӝҿ ӋҸӐӐӄҺӝࡇ ҾӍӐӑӒӎ ӈ ӢӊҿӋҿӌӑҸӋ

еӍӐӑӒӎ ӎӍ ӌҿҾӍӎӒӐӑӄӋӍӋӒ ӄӌҾҿӈӐӒ ࡫ࡷߝࡈ Ÿࡈ ߞࡁŸ࡬ ࡴ ӌҿӍӎӏҿҾҿӊҿӌӌӍҿ
ӎӍҺҿҾҿӌӄҿࡏ

T arr[N]ࡇ






0ࡏ

1ࡏ











ࡊࡏ




N-1

эӑҿӌӄҿ ӢӊҿӋҿӌӑҸ ӄӃ ӋҸӐӐӄҺҸ ӎӍ ӄӌҾҿӈӐӒ iࡇ
T value = arr[i];

кҸӎӄӐӛ ӢӊҿӋҿӌӑҸ Һ ӋҸӐӐӄҺ ӎӍ ӄӌҾҿӈӐӒ iࡇ
arr[i] = new_value;
ߠ





еҺӒӋҿӏӌӝҿ ӋҸӐӐӄҺӝ

хҿӏҺҸӨ ӏҸӃӋҿӏӌӍӐӑӛ ࡴ ӈӍӊӄӖҿӐӑҺӍ ӐӑӏӍӈࡈ ӈҸӈ Һ ӊӄӌҿӅӌӍӅ
Ҹӊһҿҹӏҿࡏ

T arr[N][M]ࡇ


0ࡏ




ࡊࡏ










M-1


ࡏࡏࡏ













N-1











0ࡏ

int m[2][3] = {
{1, 2, 3},
{4, 5, 6}};



ɾʶȩ ӎӍӊӒӖӄӑ ӃӌҸӖҿӌӄҿ ߞࡈ Ҹ ҺӋҿӐӑӍ ߢ ҹӒҾҿӑ ӃҸӎӄӐҸӌӍ ߢߢࡏ

int two = m[0][1];
m[1][2] = 66;

ߡ





тӌӍһӍӋҿӏӌӝҿ ӋҸӐӐӄҺӝ

T arr[N1][N2][...][Nk] ࡴ ҺӐҿ ҸӌҸӊӍһӄӖӌӍࡏ

ߢ





хҿӏҿҾҸӖҸ ӋҸӐӐӄҺӍҺ Һ ӔӒӌӈӗӄӄ

ɯǞˌƚ࡙ɾ ࡴ ӐӎҿӗӄҸӊӛӌӝӅ ҹҿӃӃӌҸӈӍҺӝӅ ӑӄӎ ҾҸӌӌӝӕࡈ ӈӍӑӍӏӍһӍ
һҸӏҸӌӑӄӏӍҺҸӌӌӍ ӕҺҸӑҸҿӑ ҾӊӨ ӕӏҸӌҿӌӄӨ ӏҸӃӋҿӏҸ ӊӧҹӍһӍ ӋҸӐӐӄҺҸ
ӌҸ ҾҸӌӌӍӅ ҸӏӕӄӑҿӈӑӒӏҿࡏ

кҸӖҸӐӑӒӧࡈ ӏҸӃӋҿӏ ӋҸӐӐӄҺҸ ӎҿӏҿҾҸҿӑӐӨ ӍӑҾҿӊӛӌӝӋ ӎҸӏҸӋҿӑӏӍӋࡇ
int sum_values(int values[], size_t size) {
int sum = 0;
for (size_t i = 0; i < size; i++) {
sum += values[i];
}
return sum;
}

еӊӨ ӋӌӍһӍӋҿӏӌӝӕ ӋҸӐӐӄҺӍҺ ӃӌҸӖҿӌӄӨ ҺӐҿӕ ӏҸӃӋҿӏӌӍӐӑҿӅࡈ ӈӏӍӋҿ
ӎҿӏҺӍӅ ӔӄӈӐӄӏӒӧӑӐӨࡏ

int sum_pairs(int pairs[][2], size_t size) {
// ...
}
ߣ





хҿӏҿҾҸӖҸ ӔӄӈӐӄӏӍҺҸӌӌӝӕ ӋҸӐӐӄҺӍҺ Һ ӔӒӌӈӗӄӄ
тҸӐӐӄҺӝ ӂҿӐӑӈӍ ӔӄӈӐӄӏӍҺҸӌӌӝӕ ӏҸӃӋҿӏӌӍӐӑҿӅ ӎҿӏҿҾҸӧӑӐӨ ҹҿӃ
ҾӍӎӍӊӌӄӑҿӊӛӌӝӕ ӎҸӏҸӋҿӑӏӍҺࡇ
int sum_matrix(int matrix[3][3]) {
int sum = 0;
for (size_t i = 0; i < 3; i++) {
for (size_t j = 0; j < 3; j++) {
sum += matrix[i][j];
}
}
return sum;
}

ߤ





бӍӃҺӏҸәҿӌӄҿ ӋҸӐӐӄҺӍҺ ӄӃ ӔӒӌӈӗӄӅ

ыӍӏӋҸӊӛӌӍ һӍҺӍӏӨࡈ ӔӒӌӈӗӄӨ Һ + ҺӍӍҹәҿ ӌҿ ӋӍӂҿӑ ӄӋҿӑӛ ӋҸӐӐӄҺ Һ
ӈҸӖҿӐӑҺҿ ҺӍӃҺӏҸәҸҿӋӍһӍ ӃӌҸӖҿӌӄӨࡏ
ɯǞˌƚ ࡴ ӌҿ ӋӍӂҿӑ ҹӝӑӛ ӄӐӎӍӊӛӃӍҺҸӌҸ ҾӊӨ ӄӌӄӗӄҸӊӄӃҸӗӄӄ ӋҸӐӐӄҺҸࡈ
ӑࡏ ӈࡏ ӌҿ ӨҺӊӨҿӑӐӨ ӈӍӌӐӑҸӌӑӍӅ ҺӏҿӋҿӌӄ ӈӍӋӎӄӊӨӗӄӄࡏ

int[] reverse(int arr[], size_t size) {
// compile time error
int result[size]; // compile time error
// ...
return result; // run time error
}

аӍӊҿҿ ӑӍһӍࡈ ӑҸӈӍӅ ӋҸӐӐӄҺ ӒӌӄӖӑӍӂӄӑӐӨ ӎӍӐӊҿ ҺӍӃҺӏҸәҿӌӄӨ ӄӃ
ӔӒӌӈӗӄӄࡈ ӌӍ Ӎҹ ӢӑӍӋ ӎӍӃӂҿࡏ
аӍӊҿҿ ӑӍһӍࡈ Һ ӋҸӐӐӄҺӌӒӧ ӎҿӏҿӋҿӌӌӒӧ ӌҿӊӛӃӨ ӌӄӖҿһӍ
ࢀӎӏӄӐҺӍӄӑӛࢁࡈ ӖӑӍ ҾҿӊҸӑӛ Ӑ ӏҿӃӒӊӛӑҸӑӍӋࡐ

int original[] = {1, 2, 3};
int reversed[] = reverse(original, 3);
// compile time error

ߥ





ࢀбӍӃҺӏҸәҿӌӄҿࢁ ӋҸӐӐӄҺӍҺ ӄӃ ӔӒӌӈӗӄӅ

хҿӏҿӈӊҸҾӝҺҸҿӋ ӍӑҺҿӑӐӑҺҿӌӌӍӐӑӛ ӃҸ ӐӍӃҾҸӌӄҿ ӋҸӐӐӄҺҸ ӌҸ
ҺӝӃӝҺҸӧәӒӧ ӔӒӌӈӗӄӧࡏ

void reverse(int dst[], int src[], size_t size) {
for (size_t i = 0; i < size; i++) {
dst[i] = src[size - i - 1];
}
}
int original[3] = {1, 2, 3};
int reversed[3];
reverse(reversed, original, 3);
// use reversed

ߝߜ





щӈҸӃҸӑҿӊӄ






щӈҸӃҸӑҿӊӛ ӈҸӈ ҸҾӏҿӐ
ߡߝߤ


0x20

ЯҾӏҿӐӍӋ ҹӊӍӈҸ ӎҸӋӨӑӄ ӐӖӄӑҸҿӑӐӨ ӌӍӋҿӏ ҿһӍ ӐҸӋӍһӍ ӋӊҸҾӘҿһӍ
ҹҸӅӑҸࡏ

ߟߞߜ
0x23

0x40

ǞȘɾࡁ ࡴ ӒӈҸӃҸӑҿӊӛ ӌҸ ǞȘɾࡈ ӑࡏҿࡏ ҸҾӏҿӐ ӎҸӋӨӑӄࡈ һҾҿ ӊҿӂӄӑ ǞȘɾࡏ

0x43

кҸӋҿӑӛӑҿࡈ ʿ ӎӍӋҿӌӨӊӍ ӐҺӍҿ ӃӌҸӖҿӌӄҿࡈ ӕӍӑӨ ӎӏӨӋӝӕ ӃҸӎӄӐҿӅ Һ ӌҿһӍ
ӌҿ ҹӝӊӍࡏ

int x = 518;
int y = 320;

ߝߞ





щӈҸӃҸӑҿӊӛ ӈҸӈ ҸҾӏҿӐ
ߡߝߤ


0x20
int x = 518;
int y = 320;

ЯҾӏҿӐӍӋ ҹӊӍӈҸ ӎҸӋӨӑӄ ӐӖӄӑҸҿӑӐӨ ӌӍӋҿӏ ҿһӍ ӐҸӋӍһӍ ӋӊҸҾӘҿһӍ
ҹҸӅӑҸࡏ

ߟߞߜ
0x23

0x40

ǞȘɾࡁ ࡴ ӒӈҸӃҸӑҿӊӛ ӌҸ ǞȘɾࡈ ӑࡏҿࡏ ҸҾӏҿӐ ӎҸӋӨӑӄࡈ һҾҿ ӊҿӂӄӑ ǞȘɾࡏ

0x43

кҸӋҿӑӛӑҿࡈ ʿ ӎӍӋҿӌӨӊӍ ӐҺӍҿ ӃӌҸӖҿӌӄҿࡈ ӕӍӑӨ ӎӏӨӋӝӕ ӃҸӎӄӐҿӅ Һ ӌҿһӍ
ӌҿ ҹӝӊӍࡏ

int* ptr;

ߝߞ





щӈҸӃҸӑҿӊӛ ӈҸӈ ҸҾӏҿӐ
ߡߝߤ


0x20
int x = 518;
int y = 320;

ЯҾӏҿӐӍӋ ҹӊӍӈҸ ӎҸӋӨӑӄ ӐӖӄӑҸҿӑӐӨ ӌӍӋҿӏ ҿһӍ ӐҸӋӍһӍ ӋӊҸҾӘҿһӍ
ҹҸӅӑҸࡏ

ߟߞߜ
0x23

0x40

ǞȘɾࡁ ࡴ ӒӈҸӃҸӑҿӊӛ ӌҸ ǞȘɾࡈ ӑࡏҿࡏ ҸҾӏҿӐ ӎҸӋӨӑӄࡈ һҾҿ ӊҿӂӄӑ ǞȘɾࡏ

0x43

кҸӋҿӑӛӑҿࡈ ʿ ӎӍӋҿӌӨӊӍ ӐҺӍҿ ӃӌҸӖҿӌӄҿࡈ ӕӍӑӨ ӎӏӨӋӝӕ ӃҸӎӄӐҿӅ Һ ӌҿһӍ
ӌҿ ҹӝӊӍࡏ

int* ptr;
// & - взятие адреса
// * - разыменование указателя
ptr = &x;
printf("%p %d\n", ptr, *ptr); // 0x20 518
ptr = &y;
printf("%p %d\n", ptr, *ptr); // 0x40 320

ߝߞ





щӈҸӃҸӑҿӊӛ ӈҸӈ ҸҾӏҿӐ
ߡߝߤ


0x20
int x = 518;
int y = 320;

ЯҾӏҿӐӍӋ ҹӊӍӈҸ ӎҸӋӨӑӄ ӐӖӄӑҸҿӑӐӨ ӌӍӋҿӏ ҿһӍ ӐҸӋӍһӍ ӋӊҸҾӘҿһӍ
ҹҸӅӑҸࡏ

ߟߞߜ
0x23

0x40

ǞȘɾࡁ ࡴ ӒӈҸӃҸӑҿӊӛ ӌҸ ǞȘɾࡈ ӑࡏҿࡏ ҸҾӏҿӐ ӎҸӋӨӑӄࡈ һҾҿ ӊҿӂӄӑ ǞȘɾࡏ

0x43

кҸӋҿӑӛӑҿࡈ ʿ ӎӍӋҿӌӨӊӍ ӐҺӍҿ ӃӌҸӖҿӌӄҿࡈ ӕӍӑӨ ӎӏӨӋӝӕ ӃҸӎӄӐҿӅ Һ ӌҿһӍ
ӌҿ ҹӝӊӍࡏ

int* ptr;
// & - взятие адреса
// * - разыменование указателя
ptr = &x;
printf("%p %d\n", ptr, *ptr); // 0x20 518
ptr = &y;
printf("%p %d\n", ptr, *ptr); // 0x40 320
*ptr = 999;

ߝߞ





щӈҸӃҸӑҿӊӛ ӈҸӈ ҸҾӏҿӐ
ߥߥߥ

ߡߝߤ


0x20
int x = 518;
int y = 320;

ЯҾӏҿӐӍӋ ҹӊӍӈҸ ӎҸӋӨӑӄ ӐӖӄӑҸҿӑӐӨ ӌӍӋҿӏ ҿһӍ ӐҸӋӍһӍ ӋӊҸҾӘҿһӍ
ҹҸӅӑҸࡏ

0x23

0x40

ǞȘɾࡁ ࡴ ӒӈҸӃҸӑҿӊӛ ӌҸ ǞȘɾࡈ ӑࡏҿࡏ ҸҾӏҿӐ ӎҸӋӨӑӄࡈ һҾҿ ӊҿӂӄӑ ǞȘɾࡏ

0x43

кҸӋҿӑӛӑҿࡈ ʿ ӎӍӋҿӌӨӊӍ ӐҺӍҿ ӃӌҸӖҿӌӄҿࡈ ӕӍӑӨ ӎӏӨӋӝӕ ӃҸӎӄӐҿӅ Һ ӌҿһӍ
ӌҿ ҹӝӊӍࡏ

int* ptr;
// & - взятие адреса
// * - разыменование указателя
ptr = &x;
printf("%p %d\n", ptr, *ptr); // 0x20 518
ptr = &y;
printf("%p %d\n", ptr, *ptr); // 0x40 320
*ptr = 999;

ߝߞ





щӈҸӃҸӑҿӊӛ ӈҸӈ ҸҾӏҿӐ
ߥߥߥ

ߡߝߤ


0x20
int x = 518;
int y = 320;

ЯҾӏҿӐӍӋ ҹӊӍӈҸ ӎҸӋӨӑӄ ӐӖӄӑҸҿӑӐӨ ӌӍӋҿӏ ҿһӍ ӐҸӋӍһӍ ӋӊҸҾӘҿһӍ
ҹҸӅӑҸࡏ

0x23

0x40

ǞȘɾࡁ ࡴ ӒӈҸӃҸӑҿӊӛ ӌҸ ǞȘɾࡈ ӑࡏҿࡏ ҸҾӏҿӐ ӎҸӋӨӑӄࡈ һҾҿ ӊҿӂӄӑ ǞȘɾࡏ

0x43

кҸӋҿӑӛӑҿࡈ ʿ ӎӍӋҿӌӨӊӍ ӐҺӍҿ ӃӌҸӖҿӌӄҿࡈ ӕӍӑӨ ӎӏӨӋӝӕ ӃҸӎӄӐҿӅ Һ ӌҿһӍ
ӌҿ ҹӝӊӍࡏ

int* ptr;
// & - взятие адреса
// * - разыменование указателя
ptr = &x;
printf("%p %d\n", ptr, *ptr); // 0x20 518
ptr = &y;
printf("%p %d\n", ptr, *ptr); // 0x40 320
*ptr = 999;
printf("%d %d\n", x, y); // 518 999
ߝߞ





хӏӄӋҿӏࡇ ҺӍӃҺӏҸӑ ҾҺӒӕ ӃӌҸӖҿӌӄӅ ӄӃ ӔӒӌӈӗӄӄ


0x20
void divide(
int a, int b,
int* pq,
int* pr)
{
*pq = a / b;
*pr = a % b;
}

ʾࡈ ʿ ӎҿӏҿҾҸӧӑӐӨ ӎӍ ӃӌҸӖҿӌӄӧࡈ ɛࡈ ɟ ӎҿӏҿҾҸӧӑӐӨ ӎӍ ӒӈҸӃҸӑҿӊӧࡏ

q

r

ࡐࡐࡐ

ࡐࡐࡐ
0x23

0x40

тӍҾӄӔӄӈҸӗӄӨ ŏ ӄ ų Һ ӔӒӌӈӗӄӄ ӌӄӈҸӈ ӌҿ ӄӃӋҿӌӄӑ ӃӌҸӖҿӌӄӨ ʾࡈ ʿࡏ
фҾӌҸӈӍ ӃҸӎӄӐӛ ӎӍ ӒӈҸӃҸӑҿӊӨӋ ɔɛࡈ ɔɟ ӌҿӎӍӐӏҿҾӐӑҺҿӌӌӍ ӋҿӌӨҿӑ
ӃӌҸӖҿӌӄӨ ɛࡈ ɟࡏ

0x43

int x = 123, y = 10;
int q, r;

ߝߟ





хӏӄӋҿӏࡇ ҺӍӃҺӏҸӑ ҾҺӒӕ ӃӌҸӖҿӌӄӅ ӄӃ ӔӒӌӈӗӄӄ


0x20

ʾࡈ ʿ ӎҿӏҿҾҸӧӑӐӨ ӎӍ ӃӌҸӖҿӌӄӧࡈ ɛࡈ ɟ ӎҿӏҿҾҸӧӑӐӨ ӎӍ ӒӈҸӃҸӑҿӊӧࡏ

q

r

ࡐࡐࡐ

ࡐࡐࡐ
0x23

void divide(
int a, int b, // 123, 10
int* pq, // 0x20
int* pr) // 0x40
{
*pq = a / b;
*pr = a % b;
}

0x40

тӍҾӄӔӄӈҸӗӄӨ ŏ ӄ ų Һ ӔӒӌӈӗӄӄ ӌӄӈҸӈ ӌҿ ӄӃӋҿӌӄӑ ӃӌҸӖҿӌӄӨ ʾࡈ ʿࡏ
фҾӌҸӈӍ ӃҸӎӄӐӛ ӎӍ ӒӈҸӃҸӑҿӊӨӋ ɔɛࡈ ɔɟ ӌҿӎӍӐӏҿҾӐӑҺҿӌӌӍ ӋҿӌӨҿӑ
ӃӌҸӖҿӌӄӨ ɛࡈ ɟࡏ

0x43

int x = 123, y = 10;
int q, r;
divide(x, y, &q, &r);

ߝߟ





хӏӄӋҿӏࡇ ҺӍӃҺӏҸӑ ҾҺӒӕ ӃӌҸӖҿӌӄӅ ӄӃ ӔӒӌӈӗӄӄ


0x20

ʾࡈ ʿ ӎҿӏҿҾҸӧӑӐӨ ӎӍ ӃӌҸӖҿӌӄӧࡈ ɛࡈ ɟ ӎҿӏҿҾҸӧӑӐӨ ӎӍ ӒӈҸӃҸӑҿӊӧࡏ

q

r

ࡐࡐࡐ

ࡐࡐࡐ
0x23

void divide(
int a, int b, // 123, 10
int* pq, // 0x20
int* pr) // 0x40
{
*pq = a / b; // *(0x20) = 12
*pr = a % b;
}

0x40

тӍҾӄӔӄӈҸӗӄӨ ŏ ӄ ų Һ ӔӒӌӈӗӄӄ ӌӄӈҸӈ ӌҿ ӄӃӋҿӌӄӑ ӃӌҸӖҿӌӄӨ ʾࡈ ʿࡏ
фҾӌҸӈӍ ӃҸӎӄӐӛ ӎӍ ӒӈҸӃҸӑҿӊӨӋ ɔɛࡈ ɔɟ ӌҿӎӍӐӏҿҾӐӑҺҿӌӌӍ ӋҿӌӨҿӑ
ӃӌҸӖҿӌӄӨ ɛࡈ ɟࡏ

0x43

int x = 123, y = 10;
int q, r;
divide(x, y, &q, &r);

ߝߟ





хӏӄӋҿӏࡇ ҺӍӃҺӏҸӑ ҾҺӒӕ ӃӌҸӖҿӌӄӅ ӄӃ ӔӒӌӈӗӄӄ


0x20

ʾࡈ ʿ ӎҿӏҿҾҸӧӑӐӨ ӎӍ ӃӌҸӖҿӌӄӧࡈ ɛࡈ ɟ ӎҿӏҿҾҸӧӑӐӨ ӎӍ ӒӈҸӃҸӑҿӊӧࡏ

q

r

ߝߞ

ࡐࡐࡐ
0x23

void divide(
int a, int b, // 123, 10
int* pq, // 0x20
int* pr) // 0x40
{
*pq = a / b; // *(0x20) = 12
*pr = a % b;
}

0x40

тӍҾӄӔӄӈҸӗӄӨ ŏ ӄ ų Һ ӔӒӌӈӗӄӄ ӌӄӈҸӈ ӌҿ ӄӃӋҿӌӄӑ ӃӌҸӖҿӌӄӨ ʾࡈ ʿࡏ
фҾӌҸӈӍ ӃҸӎӄӐӛ ӎӍ ӒӈҸӃҸӑҿӊӨӋ ɔɛࡈ ɔɟ ӌҿӎӍӐӏҿҾӐӑҺҿӌӌӍ ӋҿӌӨҿӑ
ӃӌҸӖҿӌӄӨ ɛࡈ ɟࡏ

0x43

int x = 123, y = 10;
int q, r;
divide(x, y, &q, &r);

ߝߟ





хӏӄӋҿӏࡇ ҺӍӃҺӏҸӑ ҾҺӒӕ ӃӌҸӖҿӌӄӅ ӄӃ ӔӒӌӈӗӄӄ


0x20

ʾࡈ ʿ ӎҿӏҿҾҸӧӑӐӨ ӎӍ ӃӌҸӖҿӌӄӧࡈ ɛࡈ ɟ ӎҿӏҿҾҸӧӑӐӨ ӎӍ ӒӈҸӃҸӑҿӊӧࡏ

q

r

ߝߞ

ࡐࡐࡐ
0x23

void divide(
int a, int b, // 123, 10
int* pq, // 0x20
int* pr) // 0x40
{
*pq = a / b; // *(0x20) = 12
*pr = a % b; // *(0x40) = 3
}

0x40

тӍҾӄӔӄӈҸӗӄӨ ŏ ӄ ų Һ ӔӒӌӈӗӄӄ ӌӄӈҸӈ ӌҿ ӄӃӋҿӌӄӑ ӃӌҸӖҿӌӄӨ ʾࡈ ʿࡏ
фҾӌҸӈӍ ӃҸӎӄӐӛ ӎӍ ӒӈҸӃҸӑҿӊӨӋ ɔɛࡈ ɔɟ ӌҿӎӍӐӏҿҾӐӑҺҿӌӌӍ ӋҿӌӨҿӑ
ӃӌҸӖҿӌӄӨ ɛࡈ ɟࡏ

0x43

int x = 123, y = 10;
int q, r;
divide(x, y, &q, &r);

ߝߟ





хӏӄӋҿӏࡇ ҺӍӃҺӏҸӑ ҾҺӒӕ ӃӌҸӖҿӌӄӅ ӄӃ ӔӒӌӈӗӄӄ


0x20

ʾࡈ ʿ ӎҿӏҿҾҸӧӑӐӨ ӎӍ ӃӌҸӖҿӌӄӧࡈ ɛࡈ ɟ ӎҿӏҿҾҸӧӑӐӨ ӎӍ ӒӈҸӃҸӑҿӊӧࡏ

q

r

тӍҾӄӔӄӈҸӗӄӨ ŏ ӄ ų Һ ӔӒӌӈӗӄӄ ӌӄӈҸӈ ӌҿ ӄӃӋҿӌӄӑ ӃӌҸӖҿӌӄӨ ʾࡈ ʿࡏ

ߝߞ

ߟ

фҾӌҸӈӍ ӃҸӎӄӐӛ ӎӍ ӒӈҸӃҸӑҿӊӨӋ ɔɛࡈ ɔɟ ӌҿӎӍӐӏҿҾӐӑҺҿӌӌӍ ӋҿӌӨҿӑ
ӃӌҸӖҿӌӄӨ ɛࡈ ɟࡏ

0x23

void divide(
int a, int b, // 123, 10
int* pq, // 0x20
int* pr) // 0x40
{
*pq = a / b; // *(0x20) = 12
*pr = a % b; // *(0x40) = 3
}

0x40

0x43

int x = 123, y = 10;
int q, r;
divide(x, y, &q, &r);

ߝߟ





хӏӄӋҿӏࡇ ҺӍӃҺӏҸӑ ҾҺӒӕ ӃӌҸӖҿӌӄӅ ӄӃ ӔӒӌӈӗӄӄ


0x20

ʾࡈ ʿ ӎҿӏҿҾҸӧӑӐӨ ӎӍ ӃӌҸӖҿӌӄӧࡈ ɛࡈ ɟ ӎҿӏҿҾҸӧӑӐӨ ӎӍ ӒӈҸӃҸӑҿӊӧࡏ

q

r

тӍҾӄӔӄӈҸӗӄӨ ŏ ӄ ų Һ ӔӒӌӈӗӄӄ ӌӄӈҸӈ ӌҿ ӄӃӋҿӌӄӑ ӃӌҸӖҿӌӄӨ ʾࡈ ʿࡏ

ߝߞ

ߟ

фҾӌҸӈӍ ӃҸӎӄӐӛ ӎӍ ӒӈҸӃҸӑҿӊӨӋ ɔɛࡈ ɔɟ ӌҿӎӍӐӏҿҾӐӑҺҿӌӌӍ ӋҿӌӨҿӑ
ӃӌҸӖҿӌӄӨ ɛࡈ ɟࡏ

0x23

void divide(
int a, int b, // 123, 10
int* pq, // 0x20
int* pr) // 0x40
{
*pq = a / b; // *(0x20) = 12
*pr = a % b; // *(0x40) = 3
}

0x40

0x43

int x = 123, y = 10;
int q, r;
divide(x, y, &q, &r);
printf("%d = %d * %d + %d\n",
x, y, q, r);
// 123 = 10 * 12 + 3
ߝߟ





хӏҿҾӐӑҸҺӊҿӌӄҿ ӒӈҸӃҸӑҿӊӨ

÷ࡁ ҾӊӨ ӊӧҹӍһӍ ÷ ҺӐҿһҾҸ ӃҸӌӄӋҸҿӑ ӍҾӄӌҸӈӍҺӍҿ ӈӍӊӄӖҿӐӑҺӍ ҹҸӅӑ ӌҸ
ӍҾӌӍӅ ӎӊҸӑӔӍӏӋҿࡏ фҹӝӖӌӍ ӑҸӈࡇ ߟߞࡷҹӄӑӌӝҿ ӎӊҸӑӔӍӏӋӝ ࡴ ߠ ҹҸӅӑҸࡈ
ߢߠࡷҹӄӑӌӝҿ ࡴ ߤ ҹҸӅӑࡏ

x


ЯҾӏҿӐ ӕӏҸӌӄӑӐӨ ӈҸӈ ҹҿӃӃӌҸӈӍҺӍҿ ӖӄӐӊӍࡏ

ߟߣ

чҿӅӖҸӐ ӌҿ ҹӒҾҿӋ ӎӍҾӏӍҹӌӍ ӍӐӑҸӌҸҺӊӄҺҸӑӛӐӨ ӌҸ ҾҺӍӅӌӝӕ
ӒӈҸӃҸӑҿӊӨӕࡏ

0x20

char x = 37;

ߝߠ





хӏҿҾӐӑҸҺӊҿӌӄҿ ӒӈҸӃҸӑҿӊӨ

÷ࡁ ҾӊӨ ӊӧҹӍһӍ ÷ ҺӐҿһҾҸ ӃҸӌӄӋҸҿӑ ӍҾӄӌҸӈӍҺӍҿ ӈӍӊӄӖҿӐӑҺӍ ҹҸӅӑ ӌҸ
ӍҾӌӍӅ ӎӊҸӑӔӍӏӋҿࡏ фҹӝӖӌӍ ӑҸӈࡇ ߟߞࡷҹӄӑӌӝҿ ӎӊҸӑӔӍӏӋӝ ࡴ ߠ ҹҸӅӑҸࡈ
ߢߠࡷҹӄӑӌӝҿ ࡴ ߤ ҹҸӅӑࡏ

px

x


ЯҾӏҿӐ ӕӏҸӌӄӑӐӨ ӈҸӈ ҹҿӃӃӌҸӈӍҺӍҿ ӖӄӐӊӍࡏ

ߟߣ
0x20

0x80

чҿӅӖҸӐ ӌҿ ҹӒҾҿӋ ӎӍҾӏӍҹӌӍ ӍӐӑҸӌҸҺӊӄҺҸӑӛӐӨ ӌҸ ҾҺӍӅӌӝӕ
ӒӈҸӃҸӑҿӊӨӕࡏ

0x83

char x = 37;
char* px = &x;

ߝߠ





хӏҿҾӐӑҸҺӊҿӌӄҿ ӒӈҸӃҸӑҿӊӨ



÷ࡁ ҾӊӨ ӊӧҹӍһӍ ÷ ҺӐҿһҾҸ ӃҸӌӄӋҸҿӑ ӍҾӄӌҸӈӍҺӍҿ ӈӍӊӄӖҿӐӑҺӍ ҹҸӅӑ ӌҸ
ӍҾӌӍӅ ӎӊҸӑӔӍӏӋҿࡏ фҹӝӖӌӍ ӑҸӈࡇ ߟߞࡷҹӄӑӌӝҿ ӎӊҸӑӔӍӏӋӝ ࡴ ߠ ҹҸӅӑҸࡈ
ߢߠࡷҹӄӑӌӝҿ ࡴ ߤ ҹҸӅӑࡏ

x

px

ߟߣ

0x20

0x20

0x80

ЯҾӏҿӐ ӕӏҸӌӄӑӐӨ ӈҸӈ ҹҿӃӃӌҸӈӍҺӍҿ ӖӄӐӊӍࡏ
чҿӅӖҸӐ ӌҿ ҹӒҾҿӋ ӎӍҾӏӍҹӌӍ ӍӐӑҸӌҸҺӊӄҺҸӑӛӐӨ ӌҸ ҾҺӍӅӌӝӕ
ӒӈҸӃҸӑҿӊӨӕࡏ

0x83

char x = 37;
char* px = &x;

ߝߠ





хӏҿҾӐӑҸҺӊҿӌӄҿ ӒӈҸӃҸӑҿӊӨ



÷ࡁ ҾӊӨ ӊӧҹӍһӍ ÷ ҺӐҿһҾҸ ӃҸӌӄӋҸҿӑ ӍҾӄӌҸӈӍҺӍҿ ӈӍӊӄӖҿӐӑҺӍ ҹҸӅӑ ӌҸ
ӍҾӌӍӅ ӎӊҸӑӔӍӏӋҿࡏ фҹӝӖӌӍ ӑҸӈࡇ ߟߞࡷҹӄӑӌӝҿ ӎӊҸӑӔӍӏӋӝ ࡴ ߠ ҹҸӅӑҸࡈ
ߢߠࡷҹӄӑӌӝҿ ࡴ ߤ ҹҸӅӑࡏ

x

px

ߟߣ

0x20

0x20

0x80

ppx
0x83

0xE0

ЯҾӏҿӐ ӕӏҸӌӄӑӐӨ ӈҸӈ ҹҿӃӃӌҸӈӍҺӍҿ ӖӄӐӊӍࡏ
чҿӅӖҸӐ ӌҿ ҹӒҾҿӋ ӎӍҾӏӍҹӌӍ ӍӐӑҸӌҸҺӊӄҺҸӑӛӐӨ ӌҸ ҾҺӍӅӌӝӕ
ӒӈҸӃҸӑҿӊӨӕࡏ

0xE4

char x = 37;
char* px = &x;
char** ppx = &px;

ߝߠ





хӏҿҾӐӑҸҺӊҿӌӄҿ ӒӈҸӃҸӑҿӊӨ



÷ࡁ ҾӊӨ ӊӧҹӍһӍ ÷ ҺӐҿһҾҸ ӃҸӌӄӋҸҿӑ ӍҾӄӌҸӈӍҺӍҿ ӈӍӊӄӖҿӐӑҺӍ ҹҸӅӑ ӌҸ
ӍҾӌӍӅ ӎӊҸӑӔӍӏӋҿࡏ фҹӝӖӌӍ ӑҸӈࡇ ߟߞࡷҹӄӑӌӝҿ ӎӊҸӑӔӍӏӋӝ ࡴ ߠ ҹҸӅӑҸࡈ
ߢߠࡷҹӄӑӌӝҿ ࡴ ߤ ҹҸӅӑࡏ

x

px

ppx

ߟߣ

0x20

0x80

0x20

0x80

0x83

0xE0

ЯҾӏҿӐ ӕӏҸӌӄӑӐӨ ӈҸӈ ҹҿӃӃӌҸӈӍҺӍҿ ӖӄӐӊӍࡏ
чҿӅӖҸӐ ӌҿ ҹӒҾҿӋ ӎӍҾӏӍҹӌӍ ӍӐӑҸӌҸҺӊӄҺҸӑӛӐӨ ӌҸ ҾҺӍӅӌӝӕ
ӒӈҸӃҸӑҿӊӨӕࡏ

0xE4

char x = 37;
char* px = &x;
char** ppx = &px;

ߝߠ





хӏҿҾӐӑҸҺӊҿӌӄҿ ӒӈҸӃҸӑҿӊӨ



÷ࡁ ҾӊӨ ӊӧҹӍһӍ ÷ ҺӐҿһҾҸ ӃҸӌӄӋҸҿӑ ӍҾӄӌҸӈӍҺӍҿ ӈӍӊӄӖҿӐӑҺӍ ҹҸӅӑ ӌҸ
ӍҾӌӍӅ ӎӊҸӑӔӍӏӋҿࡏ фҹӝӖӌӍ ӑҸӈࡇ ߟߞࡷҹӄӑӌӝҿ ӎӊҸӑӔӍӏӋӝ ࡴ ߠ ҹҸӅӑҸࡈ
ߢߠࡷҹӄӑӌӝҿ ࡴ ߤ ҹҸӅӑࡏ

x

px

ppx

ߟߣ

0x20

0x80

0x20

0x80

0x83

0xE0

ЯҾӏҿӐ ӕӏҸӌӄӑӐӨ ӈҸӈ ҹҿӃӃӌҸӈӍҺӍҿ ӖӄӐӊӍࡏ
чҿӅӖҸӐ ӌҿ ҹӒҾҿӋ ӎӍҾӏӍҹӌӍ ӍӐӑҸӌҸҺӊӄҺҸӑӛӐӨ ӌҸ ҾҺӍӅӌӝӕ
ӒӈҸӃҸӑҿӊӨӕࡏ

0xE4

char x = 37;
char* px = &x;
char** ppx = &px;
**ppx = 42;

ߝߠ





хӏҿҾӐӑҸҺӊҿӌӄҿ ӒӈҸӃҸӑҿӊӨ



÷ࡁ ҾӊӨ ӊӧҹӍһӍ ÷ ҺӐҿһҾҸ ӃҸӌӄӋҸҿӑ ӍҾӄӌҸӈӍҺӍҿ ӈӍӊӄӖҿӐӑҺӍ ҹҸӅӑ ӌҸ
ӍҾӌӍӅ ӎӊҸӑӔӍӏӋҿࡏ фҹӝӖӌӍ ӑҸӈࡇ ߟߞࡷҹӄӑӌӝҿ ӎӊҸӑӔӍӏӋӝ ࡴ ߠ ҹҸӅӑҸࡈ
ߢߠࡷҹӄӑӌӝҿ ࡴ ߤ ҹҸӅӑࡏ

x

px

ppx

ߠߞ

0x20

0x80

0x20

0x80

0x83

0xE0

ЯҾӏҿӐ ӕӏҸӌӄӑӐӨ ӈҸӈ ҹҿӃӃӌҸӈӍҺӍҿ ӖӄӐӊӍࡏ
чҿӅӖҸӐ ӌҿ ҹӒҾҿӋ ӎӍҾӏӍҹӌӍ ӍӐӑҸӌҸҺӊӄҺҸӑӛӐӨ ӌҸ ҾҺӍӅӌӝӕ
ӒӈҸӃҸӑҿӊӨӕࡏ

0xE4

char x = 37;
char* px = &x;
char** ppx = &px;
**ppx = 42;

ߝߠ





хӏҿҾӐӑҸҺӊҿӌӄҿ ӋҸӐӐӄҺҸ



тҸӐӐӄҺ ӕӏҸӌӄӑӐӨ ӈҸӈ ӌҿӎӏҿӏӝҺӌӝӅ ҹӊӍӈ ӎҸӋӨӑӄࡈ ӢӊҿӋҿӌӑӝ
ӏҸӐӎӍӊҸһҸӧӑӐӨ ӎӍӐӊҿҾӍҺҸӑҿӊӛӌӍࡏ

a[0]

a[1]

a[2]

ߝߜ

ߞߜ

ߟߜ

0x20

0x22

хӏӄ ӎӏӄҹҸҺӊҿӌӄӄ ӃӌҸӖҿӌӄӨ Ș ӈ ӒӈҸӃҸӑҿӊӧࡈ ҿһӍ ӃӌҸӖҿӌӄҿ
ӒҺҿӊӄӖӄҺҸҿӑӐӨ ӌҸ ࡫Ș ࡁ ӏҸӃӋҿӏ ӑӄӎҸ ҾҸӌӌӝӕ ӎӍҾ ӒӈҸӃҸӑҿӊҿӋ࡬ࡏ
тҸӐӐӄҺӌҸӨ ӎҿӏҿӋҿӌӌҸӨ ࡴ ҺӐҿһӍ ӊӄӘӛ ӒӈҸӃҸӑҿӊӛ ӌҸ ӌҸӖҸӊӍ
ӋҸӐӐӄҺҸࡏ

0x24

short a[3] = {10, 20, 30};

ߝߡ





хӏҿҾӐӑҸҺӊҿӌӄҿ ӋҸӐӐӄҺҸ



тҸӐӐӄҺ ӕӏҸӌӄӑӐӨ ӈҸӈ ӌҿӎӏҿӏӝҺӌӝӅ ҹӊӍӈ ӎҸӋӨӑӄࡈ ӢӊҿӋҿӌӑӝ
ӏҸӐӎӍӊҸһҸӧӑӐӨ ӎӍӐӊҿҾӍҺҸӑҿӊӛӌӍࡏ

a[0]

a[1]

a[2]

ߝߜ

ߞߜ

ߟߜ

0x20

0x22

хӏӄ ӎӏӄҹҸҺӊҿӌӄӄ ӃӌҸӖҿӌӄӨ Ș ӈ ӒӈҸӃҸӑҿӊӧࡈ ҿһӍ ӃӌҸӖҿӌӄҿ
ӒҺҿӊӄӖӄҺҸҿӑӐӨ ӌҸ ࡫Ș ࡁ ӏҸӃӋҿӏ ӑӄӎҸ ҾҸӌӌӝӕ ӎӍҾ ӒӈҸӃҸӑҿӊҿӋ࡬ࡏ
тҸӐӐӄҺӌҸӨ ӎҿӏҿӋҿӌӌҸӨ ࡴ ҺӐҿһӍ ӊӄӘӛ ӒӈҸӃҸӑҿӊӛ ӌҸ ӌҸӖҸӊӍ
ӋҸӐӐӄҺҸࡏ

0x24

short a[3] = {10, 20, 30};
short* p = &(a[0]); // 0x20
short* p1 = p + 1; // 0x22
short* p2 = p + 2; // 0x24

ߝߡ





хӏҿҾӐӑҸҺӊҿӌӄҿ ӋҸӐӐӄҺҸ



тҸӐӐӄҺ ӕӏҸӌӄӑӐӨ ӈҸӈ ӌҿӎӏҿӏӝҺӌӝӅ ҹӊӍӈ ӎҸӋӨӑӄࡈ ӢӊҿӋҿӌӑӝ
ӏҸӐӎӍӊҸһҸӧӑӐӨ ӎӍӐӊҿҾӍҺҸӑҿӊӛӌӍࡏ

a[0]

a[1]

a[2]

ߝߜ

ߞߜ

ߟߜ

0x20
short a[3] = {10, 20, 30};

0x22

хӏ