Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* see: C11 Standard - 6.3.2.1 Other Operands - Lvalues, arrays, and function designators(p3)
- * http://port70.net/~nsz/c/c11/n1570.html#6.3.2.1p3
- */
- #include <stdio.h>
- void printmyarr (int pt[][4], int nptrs)
- {
- int nelem = sizeof *pt/sizeof **pt; /* number of int per-array */
- for (int i = 0; i < nptrs; i++) { /* iterate each row */
- for (int j = 0; j < nelem; j++) /* iterate each col */
- printf (" %4d", pt[i][j]); /* output int */
- putchar ('\n'); /* tidy up with newline */
- }
- }
- void printwptr (int (*pt)[4], int nptrs)
- {
- int nelem = sizeof *pt/sizeof **pt; /* number of int per-array */
- while (nptrs--) { /* iterate nptrs times */
- int *p = *pt++, /* initialize p to row, advance pt */
- n = nelem; /* initialize n to nelem */
- while (n--) /* iterate n times */
- printf (" %4d", *p++); /* output element, advance to next */
- putchar ('\n'); /* tidy up with newline */
- }
- }
- int main (void) {
- int myarr[][4] = {{ 12, 16, 19, 20},
- { 5, 99, 102, 200},
- { 12, 20, 25, 600},
- { 65, 66, 999, 1000}};
- puts ("print with indexes:");
- printmyarr (myarr, sizeof myarr/sizeof *myarr);
- puts ("\nprint with pointers:");
- printwptr (myarr, sizeof myarr/sizeof *myarr);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement