0
int main(void) {
  int x = 0;
  int* p = &x;
  char* q = p;
  while (*p == *q) { //What happens here?
     x = x + 1;
  }
  printf(“%d\n”, x);
}

さて、ここにこの簡単なプログラムがあります。これがどのように機能するのか混乱しています。あなたが私を助けることができれば、私は義務付けられます.

私はテストを受けており、これがどのように機能するかを学びたいと思っています。前もって感謝します。

Qポインターの質問

|----------------------------|
               0x00<-------------Q ここでポイント?
|----------------------------|
               0x00
|----------------------------|
               0x00
|----------------------------|
               0x00<-------------Q ここでポイント?
|----------------------------|

x = x+1 の後です。それは...ですか?

|----------------------------|
               0x00<-------------0x01 ここ?
|----------------------------|
               0x00
|----------------------------|
               0x00
|----------------------------|
               0x00<-------------0x01 ここ?
|----------------------------|

4

2 に答える 2

3

It depends on endianness, but it's essentially:

while (x == ((char) x)) {
    ++x;
}

The loop will terminate once ((int) x) != ((int) lowest byte of x)

于 2012-09-23T00:53:30.720 に答える
1

First, p and q are dereferenced (via the *) to get the int and char they're pointing to. Then, the char is promoted to an int, and the values are compared for equality.

于 2012-09-23T00:51:51.933 に答える