初版 2026/02/06
改訂


array4.c

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

uint16_t array[2][10] = {
    {  10, 100,  20, 120,  30, 130, 255, 255, 255, 255},
    {  11,  50,  21,  60,  31,  70,  41,  80, 255, 255}};

void print_array(uint16_t n) {
    // ポインタで2次元配列にアクセス
    // 配列の先頭のポインタは、要素[0][0]を明示的に指定して取得する
    // 値を取得するには`*(ポインタ)`と書く
    uint16_t *array_p = &array[0][0] + n;

    // printfの書式:
    //   フォーマット指定子
    //   '%02d':2桁でゼロ埋め
    printf("array[%02d] value = %d\n", n, *(array_p));
    printf("array[%02d] addr  = %p\n", n, array_p);
}

void main() {
    print_array(2 + (0 * 10));
    print_array(3 + (0 * 10));
    print_array(4 + (0 * 10));
    print_array(5 + (1 * 10));
    print_array(6 + (1 * 10));
    print_array(7 + (1 * 10));

    return;
}
  • 2次元配列の先頭の要素のポインタは、&Variable[0][0]として取得する。
  • ポインタを使用して値を取得する場合は、*(Pointer)と書く(2次元ではなくあくまで1次元的にアクセスすることに注意)。


コンパイル

compile.sh

#!/bin/sh
zcc +msx -lmsxbios --list -subtype=msxdos $1.c -o $1.com


$ ./compile.sh ./array4


実行結果

2026-02-06-z88dk-c-array4_01.png

※MSXDOS.SYSとCOMMAND.COM、コンパイルして作成した.COMを配置したディレクトリをopenMSXでマウント、MSXDOSから実行