読者です 読者をやめる 読者になる 読者になる

メモ:pointer to arrayのcast

#include <stdio.h>
#include <stdlib.h>
int main() {
  int * p;
  printf("%p\n", p);
  p = malloc( sizeof(int) * 9 );
  printf("%p\n", p);
  printf("int[3] %lu\n", sizeof(int[3]));
  printf("int %lu\n", sizeof(int));

  int i, j;
  for ( i = 0; i < 9; i++ ) {
    p[i] = i;
  }

  for ( i = 0; i < 9; i++ ) {
    printf("%d ", p[i]);
  }
  printf("\n");

  int (*q)[3] = (int (*)[3])p;
  for ( i = 0; i < 3; i++ ) {
    for ( j = 0; j < 3; j++ ) {
      printf("%d ", q[i][j]);
    }
  }
  printf("\n");
}