--- title: ポインタについて author: kazu634 date: 1969-12-31 url: /1970/01/01/_37/ wordtwit_post_info: - 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:4083;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}' categories: - C - Programming ---
下の例を考えてみる。
#include <stdio.h> #define ARRAY_SIZE 10 char array[ARRAY_SIZE + 1] = "0123456789"; int main(){ int index; printf ("&array[index] (array+index) array[index]\n"); for (index = ;index < ARRAY_SIZE;index++) { printf ("0x%-10p 0x%-10p 0x%x\n", &array[index], (array + index), array[index]); } return(); }
この「(array + index)」の部分は自動的に「&array[0]」として解釈される。配列の先頭アドレスに足し算をすると、足した数分だけ要素を移動する。というわけで、「&array[index]」と「(array + index)」は等しい値 = ポインタになる。