446,194 Members | 829 Online Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

# Does this use dynamic two-dimension array correctly?

 P: n/a Do the following two functions use dynamic two-dimension array correctly? #include #include #define ROW 3 #define COL 5 int dyn2dary(void); int dyn2dary_2(void); int main(void) { dyn2dary(); printf("%c", '\n'); dyn2dary_2(); return 0; } int dyn2dary(void) { int r = 0, c = 0; char *p = malloc(ROW * COL * sizeof *p); if (!p) return -1; for (r = 0; r < ROW; r++){ for (c = 0; c < COL; c++){ p[r * COL + c] = 'a' + r * COL + c; printf("%c ", p[r * COL + c]); } printf("%c", '\n'); } free(p); return 0; } int dyn2dary_2(void) { int r = 0, c = 0; char (*a)[COL]; char *p = malloc(ROW * COL * sizeof *p); if (!p) return -1; a = (char (*) [COL])p; for (r = 0; r < ROW; r++){ for (c = 0; c < COL; c++){ a[r][c] = 'a' + r * COL + c; printf("%c ", a[r][c]); } printf("%c", '\n'); } free(a); return 0; } Jun 10 '07 #1
3 Replies

 P: n/a "lovecreatesbea...@gmail.com" #include #define ROW 3 #define COL 5 int dyn2dary(void); int dyn2dary_2(void); int main(void) { dyn2dary(); printf("%c", '\n'); dyn2dary_2(); return 0; } int dyn2dary(void) { int r = 0, c = 0; char *p = malloc(ROW * COL * sizeof *p); if (!p) return -1; for (r = 0; r < ROW; r++){ for (c = 0; c < COL; c++){ p[r * COL + c] = 'a' + r * COL + c; printf("%c ", p[r * COL + c]); } printf("%c", '\n'); } free(p); return 0; } int dyn2dary_2(void) { int r = 0, c = 0; char (*a)[COL]; char *p = malloc(ROW * COL * sizeof *p); if (!p) return -1; a = (char (*) [COL])p; for (r = 0; r < ROW; r++){ for (c = 0; c < COL; c++){ a[r][c] = 'a' + r * COL + c; printf("%c ", a[r][c]); } printf("%c", '\n'); } free(a); return 0; } I had to check the second one to see if the syntax actually worked. It is too complicated, and will break if COL is not a compile time constant, which rather defeats the purpose of a dynamic array. However you can argue that you are more likely to know the number of columns than the number of rows at compile time, and you can use 2d array access rather than calculate everything explicitly. -- Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm Jun 10 '07 #2

 P: n/a On Jun 10, 2:02 pm, "lovecreatesbea...@gmail.com" #include #define ROW 3 #define COL 5 int dyn2dary(void); int dyn2dary_2(void); int main(void) { dyn2dary(); printf("%c", '\n'); dyn2dary_2(); return 0; } int dyn2dary_2(void) { int r = 0, c = 0; char (*a)[COL]; char *p = malloc(ROW * COL * sizeof *p); if (!p) return -1; a = (char (*) [COL])p; for (r = 0; r < ROW; r++){ for (c = 0; c < COL; c++){ a[r][c] = 'a' + r * COL + c; printf("%c ", a[r][c]); } printf("%c", '\n'); } free(a); I have changed this to free(p); return 0; } Jun 10 '07 #3

 P: n/a On Sun, 10 Jun 2007 06:59:09 -0000, "lovecreatesbea...@gmail.com" On Jun 10, 2:02 pm, "lovecreatesbea...@gmail.com"Do the following two functions use dynamic two-dimension arraycorrectly?#include #include #define ROW 3#define COL 5int dyn2dary(void);int dyn2dary_2(void);int main(void){ dyn2dary(); printf("%c", '\n'); dyn2dary_2(); return 0;} >int dyn2dary_2(void){ int r = 0, c = 0; char (*a)[COL]; char *p = malloc(ROW * COL * sizeof *p); The usual idiom in this situation is a = malloc(ROW * sizeof *a); which allocates the exact same amount of space without the introduction of the extraneous p. >> if (!p) return -1; a = (char (*) [COL])p; for (r = 0; r < ROW; r++){ for (c = 0; c < COL; c++){ a[r][c] = 'a' + r * COL + c; Anally retentive nit: there is no guarantee that the expression will fit in a char. > printf("%c ", a[r][c]); Since there is no guarantee that the letters are contiguous in the character set, the value of a[r][c] may not be printable (a is not in US EBCDIC). > } printf("%c", '\n'); } free(a); I have changed this to free(p); Since a and p both point to the same byte in memory, this change makes no difference. > > return 0;} Remove del for email Jun 10 '07 #4

### This discussion thread is closed

Replies have been disabled for this discussion. 