Wednesday, December 29, 2021

C 指標入門 考試

ptr++;    // Pointer moves to the next int position (as if it was an array)

++ptr;    // Pointer moves to the next int position (as if it was an array)

++*ptr;   // The value pointed at by ptr is incremented

++(*ptr); // The value pointed at by ptr is incremented

++*(ptr); // The value pointed at by ptr is incremented

*ptr++;   // Pointer moves to the next int position (as if it was an array). But returns the old content

(*ptr)++; // The value pointed at by ptr is incremented

*(ptr)++; // Pointer moves to the next int position (as if it was an array). But returns the old content

*++ptr;   // Pointer moves to the next int position, and then get's accessed, with your code, segfault

*(++ptr); // Pointer moves to the next int position, and then get's accessed, with your code, segfault



#include "stdio.h"

int main() {
    static int x = 5;
    static int *p = &x;
    printf("(int) p   => %d\n",(int) p);
    printf("(int) p++ => %d\n",(int) p++);
    x = 5; p = &x;
    printf("(int) ++p => %d\n",(int) ++p);
    x = 5; p = &x;
    printf("++*p      => %d\n",++*p);
    x = 5; p = &x;
    printf("++(*p)    => %d\n",++(*p));
    x = 5; p = &x;
    printf("++*(p)    => %d\n",++*(p));
    x = 5; p = &x;
    printf("*p++      => %d\n",*p++);
    x = 5; p = &x;
    printf("(*p)++    => %d\n",(*p)++);
    x = 5; p = &x;
    printf("*(p)++    => %d\n",*(p)++);
    x = 5; p = &x;
    printf("*++p      => %d\n",*++p);
    x = 5; p = &x;
    printf("*(++p)    => %d\n",*(++p));
    return 0;
}

It returns

(int) p   => 256688152
(int) p++ => 256688152
(int) ++p => 256688156
++*p      => 6
++(*p)    => 6
++*(p)    => 6
*p++      => 5
(*p)++    => 5
*(p)++    => 5
*++p      => 0
*(++p)    => 0

No comments:

Post a Comment

n8n index

 【n8n免費本地端部署】Windows版|程式安裝x指令大補帖  【一鍵安裝 n8n】圖文教學,獲得無限額度自動化工具&限時免費升級企業版功能