カニゲーム攻略日記ブログ

beatmaniaIIDXやハースストーンなどのゲーム攻略日記。主にまったり勢。2016年にIIDX皆伝になった

ダブルポインタ 復習

仕事でダブルポインタを使う必要があって
ちょっと混乱したので整理した

ついでにトリプルポインタ

contents

ダブルポインタ

以下で覚えればいいかな

  • *は実体
  • &はアドレス
#include<stdio.h>

int main(void){
    int c;
    int *p;
    int **wp;

    c = 100;
    p = &c;
    wp = &p;

    printf("c=%d *p=%d **wp=%d\n", c, *p, **wp);    //cの値

    printf("&c=%p p=%p *wp=%p\n", &c, p, *wp);  //cのアドレス

    printf("&p=%p wp=%p\n", &p, wp); //pのアドレス

    return 0;
}

結果:

c=100 *p=100 **wp=100
&c=0x7ffffcbf4 p=0x7ffffcbf4 *wp=0x7ffffcbf4
&p=0x7ffffcbe8 wp=0x7ffffcbe8

トリプルポインタ

#include<stdio.h>

int main(void){
    int c;
    int *p;
    int **wp;
    int ***tp;

    c = 100;
    p = &c;
    wp = &p;
    tp = &wp;

    printf("c=%d *p=%d **wp=%d ***tp=%d\n", c, *p, **wp, ***tp);    //cの値

    printf("&c=%p p=%p *wp=%p **tp=%p\n", &c, p, *wp, **tp);  //cのアドレス

    printf("&p=%p wp=%p *tp=%p\n", &p, wp, *tp); //pのアドレス

    printf("&wp=%p tp=%p\n", &wp, tp);    //wpのアドレス

    printf("&tp=%p", &tp);  //tpのアドレス

    return 0;
}

結果:

c=100 *p=100 **wp=100 ***tp=100
&c=0x7ffffcbfc p=0x7ffffcbfc *wp=0x7ffffcbfc **tp=0x7ffffcbfc
&p=0x7ffffcbf0 wp=0x7ffffcbf0 *tp=0x7ffffcbf0
&wp=0x7ffffcbe8 tp=0x7ffffcbe8
&tp=0x7ffffcbe0