-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnatural_arrays.c
181 lines (157 loc) · 4.57 KB
/
natural_arrays.c
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static inline long get_digit_len(int base, long max)
{
const char tmp[256];
switch (base) {
case 16:
snprintf((char*)tmp, 256, "%lx", max);
break;
case 8:
snprintf((char*)tmp, 256, "%lo", max);
break;
default:
snprintf((char*)tmp, 256, "%ld", max);
break;
}
return (strlen(tmp));
}
int main()
{
long xsize, ysize, zsize;
long *table;
xsize = 5;
ysize = 4;
// This is a 1d allocated array
table = malloc(sizeof(long) * xsize * ysize);
// A bit weird of a syntax but well...
long (*t2d)[xsize][ysize] = ((long(*)[xsize][ysize])table);
// Fill the table
for (int x = 0; x < xsize * ysize; ++x)
table[x] = x;
printf("This is a demonstration on how to use variable typing in c.\n");
printf("Here we are using it to easily access arrays with continuous memory.\n\n");
printf("Here is an allocated array of size \e[35m%ld\e[0;2mx\e[0;34m%ld\e[0m \e[2m(\e[0m%ld\e[2m)\e[0m:\n", xsize, ysize, xsize * ysize);
// Format digits len for formating
long len = get_digit_len(10, xsize * ysize);
// Start reading as 1d array
printf("As 1d Array: \e[35m[\e[0m\n\t");
for (int i = 0; i < xsize * ysize; ++i)
{
printf("%*ld", (int)len, table[i]);
if (i < xsize * ysize - 1)
{
printf("\e[2m,\e[0m");
if (i % xsize == xsize - 1)
printf("\n\t");
else
printf(" ");
}
}
printf("\n\e[35m]\e[0m\n");
// Start reading as 2d array
printf("As 2d Array: \e[35m[\e[0m\n");
for (int x = 0; x < xsize; ++x)
{
printf("\t\e[34m[\e[0m ");
for (int y = 0; y < ysize; ++y)
{
printf("%*ld", (int)len, (*t2d)[x][y]);
if (y < ysize - 1)
printf("\e[2m,\e[0m ");
}
if (x < xsize - 1)
printf(" \e[34m]\e[2m,\e[0m\n");
else
printf(" \e[34m]\e[0m\n");
}
printf("\e[35m]\e[0m\n");
free(table);
printf("\n");
xsize = 3;
ysize = 4;
zsize = 5;
printf("Now let's look at this one which is \e[35m%ld\e[0;2mx\e[0;34m%ld\e[0;2mx\e[0;32m%ld\e[0m "
"\e[2m(\e[0m%ld\e[2m)\e[0m "
"\e[2m(\e[0mlong\e[2m)\e[0m"
":\n",
xsize, ysize, zsize, xsize * ysize * zsize);
// Format digits len for formating
len = get_digit_len(10, xsize * ysize * zsize);
table = malloc(sizeof(long) * xsize * ysize * zsize);
// Casting to vla pointer to use the c array natural syntax
// A bit weird of a syntax but well...
long (*t3d)[xsize][ysize][zsize] = ((long(*)[xsize][ysize][zsize])table);
for (int i = 0; i < xsize * ysize * zsize; ++i)
table[i] = i;
// Start reading as 1d array
printf("As 1d (long) Array: \e[35m[\e[0m\n\t");
for (int i = 0; i < xsize * ysize * zsize; ++i)
{
printf("%*ld", (int)len, table[i]);
if (i < xsize * ysize * zsize - 1)
{
if (i % (xsize * ysize) == xsize * ysize - 1)
printf("\e[2m,\e[0m\n\t");
else
printf("\e[2m,\e[0m ");
}
}
printf("\n\e[35m]\e[0m\n");
// Start reading as 3d array
printf("As 3d (long) Array: \e[35m[\e[0m\n"); // [
for (int x = 0; x < xsize; ++x)
{
printf("\t\e[34m[\e[0m "); // [
for (int y = 0; y < ysize; ++y)
{
printf("\e[32m[\e[0m"); // [
for (int z = 0; z < zsize; ++z)
{
// This is it right there
printf("%*ld", (int)len, (*t3d)[x][y][z]);
if (z < zsize - 1) printf("\e[2m,\e[0m"); // ,
}
printf("\e[32m]\e[0m"); // ]
if (y < ysize - 1)
printf("\e[32;2m,\e[0m "); // ,
}
printf(" \e[34m]\e[0m"); // ]
if (x < xsize - 1)
printf("\e[34;2m,\e[0m"); // ,
printf("\n"); // newline
}
printf("\e[35m]\e[0m\n"); // ]
// This is the element count so since we divided it by 2 with the type change.
// (in x86_64 anyway).
// This could be any of the axis really, I just chose x because of the printing.
zsize *= 2;
// Casting to vla pointer to use the c array natural syntax
int (*t3di)[xsize][ysize][zsize] = ((int(*)[xsize][ysize][zsize])table);
printf("\e[2mNote: Here we virtually change the size to keep reading the same amount of memory.\e[0m\n");
printf("\e[2mNote: the memory seems reversed because of the little endian of the machine.\e[0m\n");
printf("As 3d (int) Array: \e[35m[\e[0m\n"); // [
for (int x = 0; x < xsize; ++x)
{
printf("\t\e[34m[\e[0m "); // [
for (int y = 0; y < ysize; ++y)
{
printf("\e[32m[\e[0m"); // [
for (int z = 0; z < zsize; ++z)
{
// This is it right there
printf("%0*d", (int)len, (*t3di)[x][y][z]);
if (z < zsize - 1) printf("\e[2m,\e[0m"); // ,
}
printf("\e[32m]\e[0m"); // ]
if (y < ysize - 1) printf("\e[32;2m,\e[0m "); // ,
}
printf(" \e[34m]\e[0m"); // ]
if (x < xsize - 1)
printf("\e[34;2m,\e[0m"); // ,
printf("\n"); // newline
}
printf("\e[35m]\e[0m\n"); // ]
free(table);
}