initial import
[staff/goc4/2018bti7061.git] / c-4-pointers / examples / c-pointers / multidimentionarray1.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #define MAXROWS 4
5
6 int main(){
7 int cols, rows;
8 int *a[MAXROWS];
9
10 puts("Number of cols:");
11 scanf(" %d",&cols);
12 puts("Number of rows:");
13 scanf(" %d", &rows);
14
15 int row;
16 for(row = 0; row < rows ; row++){
17 a[row]= (int *) malloc (cols* sizeof(int));
18 }
19 printf("Enter table");
20
21 int r,c;
22 for(r=0;r<rows;r++){
23 for(c=0;c<cols;c++){
24 printf("val (%d,%d)=",r,c);
25 scanf(" %d",(*(a+r)+c));
26 }
27 }
28
29 for(r=0;r<rows;r++){
30 for(c=0;c<cols;c++){
31 printf(" val a[%d][%d]=%d",r,c,a[r][c]);
32
33 }
34 putchar('\n');
35 }
36 puts("other view of the same array");
37 for(r=0;r<rows;r++){
38 for(c=0;c<cols;c++){
39 printf(" val *(*(a+%d)+%d)=%d",r,c,*(*(a+r)+c));
40
41 }
42 putchar('\n');
43 }
44
45
46 /* Output
47 Number of cols:
48 2
49 Number of rows:
50 3
51 Enter tableval (0,0)=1
52 val (0,1)=2
53 val (1,0)=3
54 val (1,1)=4
55 val (2,0)=5
56 val (2,1)=6
57 val a[0][0]=1 val a[0][1]=2
58 val a[1][0]=3 val a[1][1]=4
59 val a[2][0]=5 val a[2][1]=6
60 other view of the same array
61 val *(*(a+0)+0)=1 val *(*(a+0)+1)=2
62 val *(*(a+1)+0)=3 val *(*(a+1)+1)=4
63 val *(*(a+2)+0)=5 val *(*(a+2)+1)=6
64 */
65 }