Skip to content

Instantly share code, notes, and snippets.

@hadashiA
Created July 15, 2012 12:54
Show Gist options
  • Save hadashiA/3116697 to your computer and use it in GitHub Desktop.
Save hadashiA/3116697 to your computer and use it in GitHub Desktop.
動的に確保した多次元配列のなかの、特定の要素のポインタを得る
#include <stdio.h>
#include <stdlib.h>
#define X (50)
#define Y (50)
int main(int argc, char **argv) {
int **matrix = (int **)malloc(sizeof(int *) * X);
int x, y;
for (x = 0; x < X; x++) {
matrix[x] = (int *)malloc(sizeof(int) * Y);
}
for (x = 0; x < X; x++) {
for (y = 0; y < Y; y++) {
matrix[x][y] = x + y;
}
}
printf("%d = %d\n", matrix[5][10], *(*(matrix + 5)) + 10);
printf("%p = %p\n", &matrix[5][10], (*(matrix + 5)) + 10);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment