ä¸è¨ã®ãã¼ã¸ãç¾æç¹ã§ã¯ã¦ãªããã¯ãã¼ã¯ã1241ã¤ãã¦ãã¾ããå趣æ¨ã®ãã¨ãç§ã¯1998å¹´ã«ä»¥ä¸ã®ãã¼ã¸ã«æ¸ãã¾ããã
配列とポインタの完全制覇
ãã¡ãã®ã¯ã¦ãæ°ã¯212â¦â¦ ãããã£! æãã!!
ãªã©ã¨ãã話ã¯ãã¦ããã
ä¸è¨ãã¼ã¸ã«é¢é£ãã¦ã ã¨æãã®ã§ãããTwitterã§@kinabaããã以ä¸ã®ããã«ã¤ã¶ãããã¦ããã¾ããã
ãã¨ããæ¹éã®Cã®å
¥éè¨äºãã¨ãããã¨ã§ããã°ãWebã§ã¯ãªãã®ã§URLã¯è²¼ãã¾ãããããã¨ãã°æ´ç°ææ´å
çã®ãç§ä¼ Cè¨èªåç ãã¤ã³ã¿ç·¨ã*1ã«ã¯ä»¥ä¸ã®è¨è¿°ãããã¾ã(p.22)ã
ããã§ãä¸ã®ããã«èããã¨ãint x;ã®é¨åã*ptrã«ç¸å½ããã¨èãããã¾ããã
ââ*ptrã¯intåå¤æ°ã§ããã¨è¨ã£ã¦ããããã«è§£éã§ãã¾ããã
int x ;
int *ptr ;
ã§ãå®ã®ã¨ãããããæèããä¸ã§ãç§ã¯ãCè¨èª ãã¤ã³ã¿å®å ¨å¶è¦ãã«ããæ¸ãã¾ãã(p.38)ã
å¥ã®èãæ¹ã¨ãã¦ã
ã¨ãã宣è¨ã«ã¤ãã¦ãhoge_pãhogeãæãã¦ããã¨ãã*hoge_pã¯hogeã¨åãããã«ä½¿ãã¾ãããã
int *hoge_p;ã»ãã»ãhoge_pã«*ãä»ããã¨ãintåã®å¤æ°hogeã¨åãããã«ä½¿ãããã ããããã®å®£è¨ã¯ã¤ã¾ããhoge_pã«*ãä»ãããã®ãintåã ã¨ããæå³ãªãã ã
ã¨ãã説æããã人ããã¾ãã
ãã®èãæ¹ã¯ã確ãã«ãããªãã«éç¨ããã®ã§ãã(ãã¨ãã°é åã§ãåãããã«ããã)ãã§ã¯ãã¨æ¸ãã¨ãintåã®å¤æ°ã¨ãã¦hogeã宣è¨ã§ããã®ã§ãããã?*2ââãã£ã¦ã¿ãã¨ãããã¾ãããããã¯ã·ã³ã¿ãã¯ã¹ã¨ã©ã¼ã§ãã
int *&hoge;
ããã«ããã®èãæ¹ã¯ã宣è¨ã®ä¸ã«constãå²ãè¾¼ãã§ããã¨ç ´ç¶»ãã¾ãã(å¼ã®ä¸ããconstã¯æ¸ããªã)ãé¢æ°ã¸ã®ãã¤ã³ã¿ã®å®£è¨ã§ãåé¡ãçºçãã¾ãã
ãé¢æ°ã¸ã®ãã¤ã³ã¿ã®å®£è¨ã§ãåé¡ãçºçãã¾ããã«ã¤ãã¦è£è¶³ãã¾ããä¸è¨ã®ãããªèª¬æã¯ãã使ãæã®æ§æã¨å®£è¨ã®æ§æãä¼¼ãããã«ã¨ããæ¹éã§è¨è¨ããã¦ããã¨ããåæã«å¾ãããã§ãããããã¨ãã°int *a;ã¨ãã宣è¨ããã£ãã¨ãããããã使ãæãã«*aã¨æ¸ãã¨intåã«ãªããããã¨ãããã¨ãåæã¨ãã¦ãã¾ããã§ã¯é¢æ°ã¸ã®ãã¤ã³ã¿ã®ã¨ãã«ã¯ã©ãããint (*func)(void);ã¨ãã宣è¨ããã£ãã¨ãããããã使ãæããã¿ãªãã(*func)()ã®ããã«æ¸ããã¨ããã¨*3ãããã¦ãfunc()ã¨æ¸ãã®ã§ã¯ãªãã§ãããããå°ãªãã¨ããã®ã±ã¼ã¹ã§ã¯ãã使ãæãã®æ¸ãæ¹ã¨å®£è¨ãããã¦ãã¾ãã
å®ã®ã¨ããé¢æ°ã¸ã®ãã¤ã³ã¿ãæã¡åºãã¾ã§ããªããint *a;ã¨å®£è¨ããå¤æ°ã使ãã¨ãã«ã¯a[i]ã¨æ¸ããã¨ãªã©ã¯æ®éã«ããã®ã§ããããã使ãæã®æ§æã¨å®£è¨ã®æ§æãä¼¼ãããã«ã¨ããæ¹éããããã¦å½¹ã«ç«ã¤ã¨ã¯æãã¾ãããã使ãæãã¨ããã®ã¯å¼ã®ä¸ã§ä½¿ãã¨ãããã¨ã§ããããããã®å¼ã®ä¸ã§ã¯é åããã¤ã³ã¿ã«èªã¿æ¿ããããã¨ããé¢æ°ãé¢æ°ã¸ã®ãã¤ã³ã¿ã«èªã¿æ¿ããããã¨ããé¢æ°å®ç¾©ã®ä»®å¼æ°ã®å®£è¨ã«ããã¦ã®ã¿æå¤å¨ã®ã¯ãã¤ã³ã¿ãæå³ããã¨ããè¬ã®è¦åããã£ã±ããã以ä¸ãçµå±ã"a ptr to func returning ptr to func returning char"ã®ãããªãã³ã³ãã¤ã©ã®å é¨è¡¨ç¾ã£ã½ã表示ããçµç±ããªãéããç解ã«ã¯è³ããªãã¨ç§ã¯æãã¾ãã
æåã«ç´¹ä»ãããã¼ã¸ã®ãã¯ãã³ã¡ã³ãã«ã¯ããå®éã«ãããªã³ã¼ãæ¸ãããããã¬ã¿æ¹ããããã¨æããã¨ãããã¨ãªãã typedef 使ããããã¨ãã£ãã³ã¡ã³ããã¤ãã¾ããã確ãã«signal()ã¿ãããªé¢æ°ã¯ã¡ãã£ã¨ç¹æ®ãªä¾ãã¨æãã¾ãããé©åã«typedefã使ã£ããæ§é ä½ããã¾ãããããã»ããããã®ã¯ç¢ºãã§ãããã ããtypedefã¯çµå±ãã³ã³ãã¤ã©ã®å é¨è¡¨ç¾ã£ã½ã表示ãã®ä¸é¨ã«ååãä»ãã¦ã³ããããæ©è½ã§ãããªãããã§ãã¾ãã¯å®£è¨ããã¡ãã¨èªãã¦ããªãã¨çµå±ä½¿ãããªããªãã®ã§ã¯ãªãã§ãããã((ããã¦typedefã¯æ§æä¸ãè¨æ¶ã¯ã©ã¹æå®åããªã®ã§ãtypedefèªä½ãç解ããã®ããã¶ãçµæ§é£ãã))ã
ãã¦ãæå¾ã¯å®£ä¼ãã¾ããã
ãCã®å®£è¨ã¯è±èªã§èªããã¨ãããé åã¯å¼ã®ä¸ã§ã¯ãã¤ã³ã¿ã«èªã¿å¤ãããããã¨ãããæ·»åæ¼ç®åã¯é åã¨ã¯ç¡é¢ä¿ã ãã¨ãããCã«ã¯å¤æ¬¡å é åã¯åå¨ããªããã¨ããå½¹ã«ç«ã¤è¨è¿°ã§ãã£ã±ãã§ãã!!
10å¹´åã®æ¬ã§ãããä»åãCã§ã®ãã¤ã³ã¿ã®èªã¿æ¹ãã¨ãããã¼ã¸ããããã¨ã³ããªã«ä¸ãã£ã¦ããã¨ãããã¨ã¯ããã¶ãã¾ã ããããæ¬ã®éè¦ã¯ããã®ã§ããããããã¦ä¸è¨ãã¼ã¸ã¨ãããããæ¬ã¨ãã«éè¦ãããã¨ãããã¨èªä½ãããçµå±ã®ã¨ããã³ã³ãã¤ã©ã®å é¨è¡¨ç¾ã£ã½ãã¬ãã«ã¾ã§ç解ããªãã¨Cã使ãããªããªããã¨ãããã¨ãæå³ãã¦ããã®ã ã¨æãã¾ã*4ã
*1:ãã®æ¬ã¯ä½åº¦ãæ¹çããã¦ãã¾ãããç§ä¼ï¼£è¨èªåç ãã¤ã³ã¿ç·¨ 第ï¼çãâãå³è§£ï¼£è¨èª ãã¤ã³ã¿ã®æ¥µæãâã詳解 Cè¨èª ãã¤ã³ã¿å®å ¨æ»ç¥ãã§ããã®ããªããã ããç§ãæã£ã¦ããã®ã¯ãæ°ç ç§ä¼ Cè¨èªåç ãã¤ã³ã¿ç·¨ãã®ã¿ã§ãã
*2:å®ã¯Cãå¦ã³å§ããé ã試ãããã¨ãããã¾ããç§ã
*3:æ¸ãã¾ããåãã¾ãã
*4:ããã¤ã³ã¿å®å ¨å¶è¦ãã§ã¯ãCã®åã«ã¤ãã¦é£çµãªã¹ãã®å³ã§èª¬æãã¦ãã¾ããã¾ãã«ã³ã³ãã¤ã©ã®å é¨è¡¨ç¾ã§ããããããå¿ è¦ã ã¨ç§ã¯å¤æãã¾ããã