-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathptr_v_arry.cpp
More file actions
112 lines (101 loc) · 2.58 KB
/
ptr_v_arry.cpp
File metadata and controls
112 lines (101 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#define SIZE_D1 16
#define SIZE_D2 12
#define SIZE_D3 7
using namespace std;
void foo_arry2d(char _array[][SIZE_D1])
{
for(int i=0; i<SIZE_D2 ;i++)
{
for(int j=0; j<SIZE_D1 ;j++) //now go through inside d1_ptr[i]
{
printf("%c ",_array[i][j]);//row first visit is in the most inner loop.
}
printf("\n ");
}
}
void foo_arry3d(char _array[][SIZE_D2][SIZE_D1])
{
for(int k=0; k<SIZE_D3 ;k++)
{
for(int i=0; i<SIZE_D2 ;i++) //now go through inside d1_ptr[k] total SIZE_D2*SIZE_D1 element
{
for(int j=0; j<SIZE_D1 ;j++) //now go through inside d1_ptr[k][i]
{
printf("%c ",_array[k][i][j]);//row first visit is in the most inner loop.
}
printf("\n ");
}
}
}
void foo(char* d2_arry_ptr, int d2, int d1) //flatten to char*
{
for(int i=0; i<d2 ;i++)
{
for(int j=0; j<d1 ;j++)
{
printf("%c ",(d2_arry_ptr)[j+i*d1] );
}
printf("\n ");
}
}
void foo2d(char** d2_arry_ptr, int d2, int d1)
{
for(int i=0; i<d2 ;i++)
{
for(int j=0; j<d1 ;j++)
{
printf("%c ",(*d2_arry_ptr)[j+i*d1] );
}
printf("\n ");
}
}
void foo3d(char** d3_arry_ptr, int d3, int d2, int d1)// d3_arry_ptr is flattened to an 1D array
{
for(int k=0; k<SIZE_D3 ;k++)
{
for(int i=0; i<SIZE_D2 ;i++)
{
for(int j=0; j<SIZE_D1 ;j++)
{
printf("%c ",(*d3_arry_ptr)[i*d1+j+k*d1*d2] );
}
printf("\n ");
}
printf("\n ===================================== \n");
}
}
void main()
{
const char *pInputStr="xxxyyyeeeellllllllddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddkkkkkkkkkkkkzzzzzzzzzzzzdddddddddgggggggzza";
char (*d1_ptr) [SIZE_D1] = (char (*)[SIZE_D1]) pInputStr; //then d1_ptr++ means a SIZE_D1(16) byte offset!
for(int i=0; i<SIZE_D2 ;i++)
{
for(int j=0; j<SIZE_D1 ;j++) //now go through inside d1_ptr[i]
{
printf("%c ",d1_ptr[i][j]);//row first visit is in the most inner loop.
}
printf("\n ");
}
char (*a_d1_ptr) [SIZE_D2][SIZE_D1] = (char (*)[SIZE_D2][SIZE_D1]) pInputStr;
for(int k=0; k<SIZE_D3 ;k++)
{
for(int i=0; i<SIZE_D2 ;i++)
{
for(int j=0; j<SIZE_D1 ;j++) //now go through inside d1_ptr[i]
{
printf("%c ",a_d1_ptr[k][i][j]);//row first visit is in the most inner loop.
}
printf("\n ");
}
printf("\n ===================================== \n");
}
foo((char*)d1_ptr,SIZE_D2, SIZE_D1);
foo2d((char**)&d1_ptr, SIZE_D2, SIZE_D1);
foo3d((char**)&d1_ptr, SIZE_D3,SIZE_D2, SIZE_D1);
foo_arry2d(d1_ptr);
foo_arry3d(a_d1_ptr);
printf("\n ");
}