1

我最近收到了这段代码作为家庭作业进行分析(最近我的意思是大约 3 周前)。我们应该逐行检查代码并确定打印语句(无需编译/运行)。

我大约完成了一半,然后指针开始操作一个整数数组,我再也无法跟踪它了。谁能帮助解释正在操纵哪些价值观以及为什么?

我想这段代码和一个好的解释不仅对我有很大的帮助,对其他来自没有指针的语言的人也有很大的帮助。

这是代码(请注意,评论是我自己的,而不是讲师的):

int main() {
  // Pointers (* var_name) point to an address in memory. They can be
  // dereferenced by using (* var_name) during assignment. The operator (&)
  // gives the address in memory of a variable, which is useful for assigning
  // pointers to (point) to that location in memory (a type of assignment).

  int i = 50, j = 100;
  int k[] = {10, 20, 30, 40, 50};

  // Declare a pointer to p, and a pointer to q
  int *p, *q;

  // Declare a pointer to a pointer s
  int **s;

  // Declare a pointer to a pointer to a pointer t
  int ***t;

  // Since p points to an address in memory, and & gives the address in memory,
  // assign p to be the same memory location as i
  p = &i;

  printf("%d %d\n", i, *p);

  // Since q points to an address in memory, and & gives the address in memory,
  // assign q to be the same memory location as j
  q = &j;

  printf("%d %d\n", j, *q);

  // Since s points to a pointer to an address in memory, and & gives the address 
  // in memory, assign s to be the same memory location as p
  s = &p;

  printf("%d %d\n", *p, **s);

  // Since t points to a pointer to a pointer to an address in memory, and & gives 
  // the address in memory, assign t to be the same memory location as s. Since s
  // points to the same location as p, t now points to the same location as t.
  t = &s;

  printf("%d %d %d %d\n", *p, *q, **s, ***t);

  // The pointer p now has the same reference (in memory) as the pointer q
  p = q;

  printf("%d %d %d %d\n", *p, *q, **s, ***t);

  // Since s points to a pointer to an address in memory, and & gives the address 
  // in memory, assign s to be the same memory location as q. Since p and q point
  // to the same location, an equivalent statement is (s = &p)
  s = &q;

  printf("%d %d %d %d\n", *p, *q, **s, ***t);

  /*
   * This is where I get seriously lost, and I really have no clue.
   */
  // p now points to the first element of k, because the first element of k is
  // equivalent to *k (they are both pointers)
  p = k;
  *p = *q;
  p++;
  s = &p;
  **s = 500;
  q = p + 2;
  *q = 1000;
  p = q - 1;
  *p = 750;
  q ++;
  *q = *q * 100;

  printf("%d %d %d %d %d\n", k[0], k[1], k[2], k[3], k[4]);

  **s = -100;
  ***t = -200;

  printf("%d %d %d %d %d\n", k[0], k[1], k[2], k[3], k[4]);

  printf("%d %d %d %d %d %d\n", i, j, *p, *q, **s, ***t);

  return 0;
}

编译运行后,输出如下(供参考):

50 50

100 100

50 50

50 100 50 50

100 100 100 100

100 100 100 100

100 500 750 1000 5000

100 500 -200 1000 5000

50 100 -200 5000 -200 -200

所以我想知道,在大约 10 个操作的巨大块中开始发生什么?我跟不上。另外,如果我在代码中的注释有误,请告诉我,这只是我的理解。

PS-我知道该语句p = k将指针分配给指向p的第一个元素int[] k,但我不知道从那时起会发生什么k...

4

3 回答 3

5

所以,让我们一步一步来。我只会向您展示内存操作,并让您了解正在打印的内容。每个内存位置(框)上方是其各自的地址,内部是其内容。

请注意,变量上方的内存地址是指示性的。不能保证变量会以这种方式放置在堆栈中(很可能不会)。

int *p, *q;

在这里,您没有声明“指向por的指针q”,而是声明了 2 个指向int、 称为pand的指针q

int **s;
int ***t;

同样,指向int被调用指针的指针和指向被调用指针s的指针。intt

所以让我们“绘制”到目前为止的记忆。

  i: 0x1000    j: 0x1004
    +------+     +------+
    |  50  |     |  100 |
    +------+     +------+

  k: 0x1008       0x100C 0x1010 0x1014 0x1018 0x101C
    +------+     +------+------+------+------+------+
    |0x100C|---->|  10  |  20  |  30  |  40  |  50  |
    +------+     +------+------+------+------+------+

  p: 0x1020    q: 0x1024    s: 0x1028    t: 0x102C
    +------+     +------+     +------+     +------+
    |XXXXXX|     |XXXXXX|     |XXXXXX|     |XXXXXX|
    +------+     +------+     +------+     +------+

现在,让我们运行代码:

p = &i;

现在p指向i

  i: 0x1000    j: 0x1004
    +------+     +------+
+-->|  50  |     |  100 |
|   +------+     +------+
|
| k: 0x1008       0x100C 0x1010 0x1014 0x1018 0x101C
|   +------+     +------+------+------+------+------+
|   |0x100C|---->|  10  |  20  |  30  |  40  |  50  |
|   +------+     +------+------+------+------+------+
|
| p: 0x1020    q: 0x1024    s: 0x1028    t: 0x102C
|   +------+     +------+     +------+     +------+
+---|0x1000|     |XXXXXX|     |XXXXXX|     |XXXXXX|
    +------+     +------+     +------+     +------+

下一个命令是:

q = &j;

现在q指向j

  i: 0x1000    j: 0x1004
    +------+     +------+
+-->|  50  |     |  100 |<------------------------------------+
|   +------+     +------+                                     |
|                                                             |
| k: 0x1008       0x100C 0x1010 0x1014 0x1018 0x101C          |
|   +------+     +------+------+------+------+------+         |
|   |0x100C|---->|  10  |  20  |  30  |  40  |  50  |         |
|   +------+     +------+------+------+------+------+         |
|                                                             |
| p: 0x1020    q: 0x1024    s: 0x1028    t: 0x102C            |
|   +------+     +------+     +------+     +------+           |
+---|0x1000|     |0x1004|-+   |XXXXXX|     |XXXXXX|           |
    +------+     +------+ |   +------+     +------+           |
                          |                                   |
                          +-----------------------------------+

接下来我们有:

s = &p;

s是指向指针的指针,int并且p是指向 的指针int。所以现在s指向p

  i: 0x1000    j: 0x1004
    +------+     +------+
+-->|  50  |     |  100 |<------------------------------------+
|   +------+     +------+                                     |
|                                                             |
| k: 0x1008       0x100C 0x1010 0x1014 0x1018 0x101C          |
|   +------+     +------+------+------+------+------+         |
|   |0x100C|---->|  10  |  20  |  30  |  40  |  50  |         |
|   +------+     +------+------+------+------+------+         |
|                                                             |
| p: 0x1020    q: 0x1024    s: 0x1028    t: 0x102C            |
|   +------+     +------+     +------+     +------+           |
+---|0x1000|     |0x1004|-+   |0x1020|     |XXXXXX|           |
    +------+     +------+ |   +------+     +------+           |
        ^                 |      |                            |
        |                 +------+----------------------------+
        |                        |
        +------------------------+

接下来:

t = &s;

t是指向指针的指针,int并且s是指向指针的指针int。所以现在t指向s

  i: 0x1000    j: 0x1004
    +------+     +------+
+-->|  50  |     |  100 |<------------------------------------+
|   +------+     +------+                                     |
|                                                             |
| k: 0x1008       0x100C 0x1010 0x1014 0x1018 0x101C          |
|   +------+     +------+------+------+------+------+         |
|   |0x100C|---->|  10  |  20  |  30  |  40  |  50  |         |
|   +------+     +------+------+------+------+------+         |
|                                                             |
| p: 0x1020    q: 0x1024    s: 0x1028    t: 0x102C            |
|   +------+     +------+     +------+     +------+           |
+---|0x1000|     |0x1004|-+   |0x1020|<----|0x1028|           |
    +------+     +------+ |   +------+     +------+           |
        ^                 |      |                            |
        |                 +------+----------------------------+
        |                        |
        +------------------------+

下一个:

p = q;

我们将 的内容分配qp。这意味着p不再指向i,而是现在指向指向的位置q,即j

  i: 0x1000    j: 0x1004
    +------+     +------+
    |  50  |  +->|  100 |<------------------------------------+
    +------+  |  +------+                                     |
+-------------+                                               |
| k: 0x1008       0x100C 0x1010 0x1014 0x1018 0x101C          |
|   +------+     +------+------+------+------+------+         |
|   |0x100C|---->|  10  |  20  |  30  |  40  |  50  |         |
|   +------+     +------+------+------+------+------+         |
|                                                             |
| p: 0x1020    q: 0x1024    s: 0x1028    t: 0x102C            |
|   +------+     +------+     +------+     +------+           |
+---|0x1004|     |0x1004|-+   |0x1020|<----|0x1028|           |
    +------+     +------+ |   +------+     +------+           |
        ^                 |      |                            |
        |                 +------+----------------------------+
        |                        |
        +------------------------+

下一个:

s = &q;

我们分配s指向 的地址q。所以,现在s不再指向p,而是指向q

  i: 0x1000    j: 0x1004
    +------+     +------+
    |  50  |  +->|  100 |<------------------------------------+
    +------+  |  +------+                                     |
+-------------+                                               |
| k: 0x1008       0x100C 0x1010 0x1014 0x1018 0x101C          |
|   +------+     +------+------+------+------+------+         |
|   |0x100C|---->|  10  |  20  |  30  |  40  |  50  |         |
|   +------+     +------+------+------+------+------+         |
|                                                             |
| p: 0x1020    q: 0x1024    s: 0x1028    t: 0x102C            |
|   +------+     +------+     +------+     +------+           |
+---|0x1004|     |0x1004|-+   |0x1024|<----|0x1028|           |
    +------+     +------+ |   +------+     +------+           |
                     ^    |      |                            |
                     |    +------+----------------------------+
                     |           |
                     +-----------+

下一个:

p = k;

我们将 的内容分配kp。所以现在p指向指向的位置k,也就是数组的第一个元素:

  i: 0x1000    j: 0x1004
    +------+     +------+
    |  50  |     |  100 |<------------------------------------+
    +------+     +------+                                     |
+-------------+                                               |
| k: 0x1008   |   0x100C 0x1010 0x1014 0x1018 0x101C          |
|   +------+  +->+------+------+------+------+------+         |
|   |0x100C|---->|  10  |  20  |  30  |  40  |  50  |         |
|   +------+     +------+------+------+------+------+         |
|                                                             |
| p: 0x1020    q: 0x1024    s: 0x1028    t: 0x102C            |
|   +------+     +------+     +------+     +------+           |
+---|0x100C|     |0x1004|-+   |0x1024|<----|0x1028|           |
    +------+     +------+ |   +------+     +------+           |
                     ^    |      |                            |
                     |    +------+----------------------------+
                     |           |
                     +-----------+

下一个:

*p = *q;

该命令说:“获取指向的位置的内容q并将其分配给指向的位置p”。q指向j等于 100 的 ,p指向数组的第一个元素:

  i: 0x1000    j: 0x1004
    +------+     +------+
    |  50  |     |  100 |<------------------------------------+
    +------+     +------+                                     |
+-------------+                                               |
| k: 0x1008   |   0x100C 0x1010 0x1014 0x1018 0x101C          |
|   +------+  +->+------+------+------+------+------+         |
|   |0x100C|---->|  100 |  20  |  30  |  40  |  50  |         |
|   +------+     +------+------+------+------+------+         |
|                                                             |
| p: 0x1020    q: 0x1024    s: 0x1028    t: 0x102C            |
|   +------+     +------+     +------+     +------+           |
+---|0x100C|     |0x1004|-+   |0x1024|<----|0x1028|           |
    +------+     +------+ |   +------+     +------+           |
                     ^    |      |                            |
                     |    +------+----------------------------+
                     |           |
                     +-----------+

下一个:

p++;

这是“棘手”的命令(尽管当您意识到发生了什么时并不那么棘手)。在正常情况下,这将使p变量增加 1。但由于p是指针,它将增加它(应该)指向的变量类型的大小,从而p指向下一个内存位置。在这种情况下sizeof(int),假设是 4(这不一定是真的)。所以现在p指向数组的下一个单元:

  i: 0x1000    j: 0x1004
    +------+     +------+
    |  50  |     |  100 |<------------------------------------+
    +------+     +------+                                     |
+-------------+                                               |
| k: 0x1008   |   0x100C 0x1010 0x1014 0x1018 0x101C          |
|   +------+  |  +------+------+------+------+------+         |
|   |0x100C|--+->|  100 |  20  |  30  |  40  |  50  |         |
|   +------+  |  +------+------+------+------+------+         |
|             +-------------^                                 |
| p: 0x1020    q: 0x1024    s: 0x1028    t: 0x102C            |
|   +------+     +------+     +------+     +------+           |
+---|0x1010|     |0x1004|-+   |0x1024|<----|0x1028|           |
    +------+     +------+ |   +------+     +------+           |
                     ^    |      |                            |
                     |    +------+----------------------------+
                     |           |
                     +-----------+

下一个:

s = &p

s指向 的位置p

  i: 0x1000    j: 0x1004
    +------+     +------+
    |  50  |     |  100 |<------------------------------------+
    +------+     +------+                                     |
+-------------+                                               |
| k: 0x1008   |   0x100C 0x1010 0x1014 0x1018 0x101C          |
|   +------+  |  +------+------+------+------+------+         |
|   |0x100C|--+->|  100 |  20  |  30  |  40  |  50  |         |
|   +------+  |  +------+------+------+------+------+         |
|             +-------------^                                 |
| p: 0x1020    q: 0x1024    s: 0x1028    t: 0x102C            |
|   +------+     +------+     +------+     +------+           |
+---|0x1010|     |0x1004|-+   |0x1020|<----|0x1028|           |
    +------+     +------+ |   +------+     +------+           |
        ^                 |      |                            |
        |                 +------+----------------------------+
        |                        |
        +------------------------+

下一个:

**s = 500;

该命令说:“转到指向的位置s并获取其内容,我们称之为*s,然后转到指向的位置*s并分配值 500。s指向p,并p指向第二个数组单元格:

  i: 0x1000    j: 0x1004
    +------+     +------+
    |  50  |     |  100 |<------------------------------------+
    +------+     +------+                                     |
+-------------+                                               |
| k: 0x1008   |   0x100C 0x1010 0x1014 0x1018 0x101C          |
|   +------+  |  +------+------+------+------+------+         |
|   |0x100C|--+->|  100 |  500 |  30  |  40  |  50  |         |
|   +------+  |  +------+------+------+------+------+         |
|             +-------------^                                 |
| p: 0x1020    q: 0x1024    s: 0x1028    t: 0x102C            |
|   +------+     +------+     +------+     +------+           |
+---|0x1010|     |0x1004|-+   |0x1020|<----|0x1028|           |
    +------+     +------+ |   +------+     +------+           |
        ^                 |      |                            |
        |                 +------+----------------------------+
        |                        |
        +------------------------+

下一个:

q = p + 2;

同样在这里,如上所述,因为qandp是指针,所以这个命令分配给plusq的内容。指向第二个数组单元,所以现在指向第 4 个数组单元:p2 * sizeof(int)pq

  i: 0x1000    j: 0x1004
    +------+     +------+
    |  50  |     |  100 |                 +-------------------+
    +------+     +------+                 |                   |
+-------------+                           v                   |
| k: 0x1008   |   0x100C 0x1010 0x1014 0x1018 0x101C          |
|   +------+  |  +------+------+------+------+------+         |
|   |0x100C|--+->|  100 |  500 |  30  |  40  |  50  |         |
|   +------+  |  +------+------+------+------+------+         |
|             +-------------^                                 |
| p: 0x1020    q: 0x1024    s: 0x1028    t: 0x102C            |
|   +------+     +------+     +------+     +------+           |
+---|0x1010|     |0x1018|-+   |0x1020|<----|0x1028|           |
    +------+     +------+ |   +------+     +------+           |
        ^                 |      |                            |
        |                 +------+----------------------------+
        |                        |
        +------------------------+

下一个:

*q = 1000;

q转到指向的位置并分配1000:

  i: 0x1000    j: 0x1004
    +------+     +------+
    |  50  |     |  100 |                 +-------------------+
    +------+     +------+                 |                   |
+-------------+                           v                   |
| k: 0x1008   |   0x100C 0x1010 0x1014 0x1018 0x101C          |
|   +------+  |  +------+------+------+------+------+         |
|   |0x100C|--+->|  100 |  500 |  30  | 1000 |  50  |         |
|   +------+  |  +------+------+------+------+------+         |
|             +-------------^                                 |
| p: 0x1020    q: 0x1024    s: 0x1028    t: 0x102C            |
|   +------+     +------+     +------+     +------+           |
+---|0x1010|     |0x1018|-+   |0x1020|<----|0x1028|           |
    +------+     +------+ |   +------+     +------+           |
        ^                 |      |                            |
        |                 +------+----------------------------+
        |                        |
        +------------------------+

下一个:

p = q - 1;

p现在将指向 where 指向的先前位置p,即第三个数组单元:

  i: 0x1000    j: 0x1004
    +------+     +------+
    |  50  |     |  100 |                 +-------------------+
    +------+     +------+                 |                   |
+-------------+                           v                   |
| k: 0x1008   |   0x100C 0x1010 0x1014 0x1018 0x101C          |
|   +------+  |  +------+------+------+------+------+         |
|   |0x100C|--+->|  100 |  500 |  30  | 1000 |  50  |         |
|   +------+  |  +------+------+------+------+------+         |
|             +-------------------^                           |
| p: 0x1020    q: 0x1024    s: 0x1028    t: 0x102C            |
|   +------+     +------+     +------+     +------+           |
+---|0x1014|     |0x1018|-+   |0x1020|<----|0x1028|           |
    +------+     +------+ |   +------+     +------+           |
        ^                 |      |                            |
        |                 +------+----------------------------+
        |                        |
        +------------------------+

下一个:

*p = 750;

转到p指向并分配750的位置:

  i: 0x1000    j: 0x1004
    +------+     +------+
    |  50  |     |  100 |                 +-------------------+
    +------+     +------+                 |                   |
+-------------+                           v                   |
| k: 0x1008   |   0x100C 0x1010 0x1014 0x1018 0x101C          |
|   +------+  |  +------+------+------+------+------+         |
|   |0x100C|--+->|  100 |  500 |  750 | 1000 |  50  |         |
|   +------+  |  +------+------+------+------+------+         |
|             +-------------------^                           |
| p: 0x1020    q: 0x1024    s: 0x1028    t: 0x102C            |
|   +------+     +------+     +------+     +------+           |
+---|0x1014|     |0x1018|-+   |0x1020|<----|0x1028|           |
    +------+     +------+ |   +------+     +------+           |
        ^                 |      |                            |
        |                 +------+----------------------------+
        |                        |
        +------------------------+

下一个:

q++;

同上,移动q指向它所指向的下一个内存位置,即数组的第 5 个单元:

  i: 0x1000    j: 0x1004
    +------+     +------+
    |  50  |     |  100 |                        +------------+
    +------+     +------+                        |            |
+-------------+                                  v            |
| k: 0x1008   |   0x100C 0x1010 0x1014 0x1018 0x101C          |
|   +------+  |  +------+------+------+------+------+         |
|   |0x100C|--+->|  100 |  500 |  750 | 1000 |  50  |         |
|   +------+  |  +------+------+------+------+------+         |
|             +-------------------^                           |
| p: 0x1020    q: 0x1024    s: 0x1028    t: 0x102C            |
|   +------+     +------+     +------+     +------+           |
+---|0x1014|     |0x101C|-+   |0x1020|<----|0x1028|           |
    +------+     +------+ |   +------+     +------+           |
        ^                 |      |                            |
        |                 +------+----------------------------+
        |                        |
        +------------------------+

下一个:

*q = *q * 100;

转到指向的位置q并获取其内容 (50)。乘以 100 (=5000)。最后将此值分配给指向的位置q

  i: 0x1000    j: 0x1004
    +------+     +------+
    |  50  |     |  100 |                        +------------+
    +------+     +------+                        |            |
+-------------+                                  v            |
| k: 0x1008   |   0x100C 0x1010 0x1014 0x1018 0x101C          |
|   +------+  |  +------+------+------+------+------+         |
|   |0x100C|--+->|  100 |  500 |  750 | 1000 | 5000 |         |
|   +------+  |  +------+------+------+------+------+         |
|             +-------------------^                           |
| p: 0x1020    q: 0x1024    s: 0x1028    t: 0x102C            |
|   +------+     +------+     +------+     +------+           |
+---|0x1014|     |0x101C|-+   |0x1020|<----|0x1028|           |
    +------+     +------+ |   +------+     +------+           |
        ^                 |      |                            |
        |                 +------+----------------------------+
        |                        |
        +------------------------+

下一个:

**s = -100;

s到指向 ( )的位置,p获取它的内容,我们称之为*s。现在转到*s指向(第 3 个数组单元)的位置并分配 -100:

  i: 0x1000    j: 0x1004
    +------+     +------+
    |  50  |     |  100 |                        +------------+
    +------+     +------+                        |            |
+-------------+                                  v            |
| k: 0x1008   |   0x100C 0x1010 0x1014 0x1018 0x101C          |
|   +------+  |  +------+------+------+------+------+         |
|   |0x100C|--+->|  100 |  500 | -100 | 1000 | 5000 |         |
|   +------+  |  +------+------+------+------+------+         |
|             +-------------------^                           |
| p: 0x1020    q: 0x1024    s: 0x1028    t: 0x102C            |
|   +------+     +------+     +------+     +------+           |
+---|0x1014|     |0x101C|-+   |0x1020|<----|0x1028|           |
    +------+     +------+ |   +------+     +------+           |
        ^                 |      |                            |
        |                 +------+----------------------------+
        |                        |
        +------------------------+

下一个:

***t = -200;

转到t指向 ( s) 的位置并获取其内容,我们称之为*t。现在转到*t指向 ( q) 的位置并获取其内容,我们称之为**t。现在转到指向的位置**t(再次是数组的第 3 个单元格)并分配 -200:

  i: 0x1000    j: 0x1004
    +------+     +------+
    |  50  |     |  100 |                        +------------+
    +------+     +------+                        |            |
+-------------+                                  v            |
| k: 0x1008   |   0x100C 0x1010 0x1014 0x1018 0x101C          |
|   +------+  |  +------+------+------+------+------+         |
|   |0x100C|--+->|  100 |  500 | -200 | 1000 | 5000 |         |
|   +------+  |  +------+------+------+------+------+         |
|             +-------------------^                           |
| p: 0x1020    q: 0x1024    s: 0x1028    t: 0x102C            |
|   +------+     +------+     +------+     +------+           |
+---|0x1014|     |0x101C|-+   |0x1020|<----|0x1028|           |
    +------+     +------+ |   +------+     +------+           |
        ^                 |      |                            |
        |                 +------+----------------------------+
        |                        |
        +------------------------+

我希望这个答案可以为您和其他人澄清事情。在学习和玩指针时总是画出记忆,它会让你的生活更轻松。

问候。

于 2013-10-25T12:09:45.540 回答
1

它有助于绘制图形来可视化事物。我在下面添加了解决方案,但要自己弄清楚,只需在图中绘制所有关系。

p = k;  // p now points at the first element of k (value=10)
*p = *q;  // replace first element in k with *q (value=100) -> array now 100, 20, 30, ..
p++;  // p now points to the second element of k
s = &p; // s points at p, p points at second element of k
**s = 500; // replace second element of k with 500 -> 100, 500, 30,..
q = p + 2; // q now points at 4th element of k (value=40)
*q = 1000;  // set value to 1000 -> 100, 500, 30, 1000, 50
p = q - 1; // p now points to 3rd element
*p = 750; // set to 750 -> 100, 500, 750, 1000, 50
q++; // increase q, now points at 5th element
*q = *q * 100; // multiply value of 5th element by 100 -> 100, 500, 750, 1000, 5000

**s = -100;  // s points to p, which points to 3rd element -> 100, 500, -100, 1000, 5000
***t = -200; // t points to s, so replace same value -> 100, 500, -200, 1000, 5000
于 2013-10-25T11:07:28.253 回答
0
int main() {
  int i = 50, j = 100;
  int k[] = {10, 20, 30, 40, 50};
  int *p, *q;
  int **s;
  int ***t;
  p = &i;
  printf("%d %d\n", i, *p); //50 50
  q = &j;
  printf("%d %d\n", j, *q); //100 100
  s = &p;
  printf("%d %d\n", *p, **s); // 50 50
  t = &s;
  printf("%d %d %d %d\n", *p, *q, **s, ***t);
  p = q;
  printf("%d %d %d %d\n", *p, *q, **s, ***t);
  s = &q;
  printf("%d %d %d %d\n", *p, *q, **s, ***t);

  // k is a pointer to a memory where the array is stored
  // in this case a memory with values [10,20,30,40,50]
  // p will also point to this memory address
  p = k;
  // *q is j , p is pointing to the array => *p will be the first element of the array
  // *p = *q => firts element of array will be j 
  // the new array will be [100,20,30,40,50]
  *p = *q;

  //in case of pointers, the ++ oprtator will act like this : p = p + sizeof(*p) wich means next int at that memory location, in our case, next element in array
  //if we have p+2 , it will be translated as p + 2*sizeof(*p) which means the 3rd element of the array (k[2])
  p++;
  //s is pointer to p, p is pointer to 2nd element of array (k[1])
  s = &p;
  // **s = *p = k[1], our array will be [100,500,30,40,50]
  **s = 500;
  //now, we move p, 2 elements forwards, that means p = &k[3]
  //q will point to k[3]
  q = p + 2;
  //k[3] will be 1000=> [100,500,30,1000,50]
  *q = 1000;
  //p will be &k[3] -1 => &k[2]
  p = q - 1;
  // k[2] = 750 => [100,500,750,1000,50]
  *p = 750;
  // q is &k[3], q++ will be next element , &k[4]
  q ++;
  // *(&k[4]) = *(&k[4]) * 100 => k[4] = k[4] * 100 => [100.500,750,1000,5000]
  *q = *q * 100;

  printf("%d %d %d %d %d\n", k[0], k[1], k[2], k[3], k[4]);

  //s is &p (from line 33), p is k[2] => **s => *(*(&p)) =>*(*(&(&k[2])) => k[2]
  // k[2] = -100
  **s = -100;
  //t is &s => *(*(*(*(&s)))) => *(*(*(&(&(&k[2]))))) => k[2]
  // k[2] = -200
  ***t = -200;

  printf("%d %d %d %d %d\n", k[0], k[1], k[2], k[3], k[4]);

  //i is 50
  //j is 100
  //*p = *(&k[2]) = k[2] = -200
  //*q = *(&k[4]) = k[4] = 5000
  //**s = *(*(&p)) = **&&k[2] = -200
  //***t = ***&s = **s = -200
  printf("%d %d %d %d %d %d\n", i, j, *p, *q, **s, ***t);

  return 0;
}
于 2013-10-25T11:45:57.517 回答