Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
2-dimension array got area by malloc()
#include <stdio.h> #include <stdlib.h> #define ID_GLOBAL 1111 //#define DEBUG #if !defined(DEBUG) #define xmalloc(n, m) malloc(n) #define xfree(p, m) free(p) #endif void *dim_alloc(int m, int n) { void *p = xmalloc(sizeof(int *) * m + sizeof(int) * m * n, ID_GLOBAL); for (int i = 0; i < m; i++) ((int **)p)[i] = p + m * sizeof(int *) + i * n * sizeof(int); return p; } int main() { int m = 3, n = 4; int **p = (int **)dim_alloc(m, n); for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) p[i][j] = (i + 1) * (j + 1); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) printf("%-2d ", p[i][j]); putchar('\n'); } xfree(p, ID_GLOBAL); return 0; } /* end */
run
|
edit
|
history
|
help
1
Please
log in
to post a comment.
time clock and day count through seconds by ratneshgujarathi
pointer_swap
Project 3 part 2 Book v0.7
NOT a String in C
null p null q
Assignment 3 Final
decimalespi
C_141210_Tartaglia
1
Assignment 5
Please log in to post a comment.