dump c array
#include <stdio.h>
#include <stdlib.h>
void dump_arr(unsigned int sz, unsigned int idx)
{
unsigned int end;
if (sz <= 0 || idx < 0)
return ;
idx = (idx >= sz) ? (sz - 1) : idx;
end = idx;
printf("sz: %u idx: %u end: %u\n", sz, idx, end);
do {
printf("%u\n", idx);
idx = (idx == 0) ? (sz - 1) : (idx - 1);
} while (idx != end);
}
int main(int argc, char *argv[])
{
unsigned int sz = 0, idx = 0;
if (argc <= 2) {
printf("Usage: ./test <x> <y>\n");
return 0;
}
sz = atoi(argv[1]);
idx = atoi(argv[2]);
dump_arr(sz, idx);
return 0;
}
jiaguo@jiaguo-VirtualBox:~/Desktop$ ./test 10 7
sz: 10 idx: 7 end: 7
7
6
5
4
3
2
1
0
9
8
jiaguo@jiaguo-VirtualBox:~/Desktop$ ./test 10 12
sz: 10 idx: 9 end: 9
9
8
7
6
5
4
3
2
1
0
jiaguo@jiaguo-VirtualBox:~/Desktop$ ./test 10 1
sz: 10 idx: 1 end: 1
1
0
9
8
7
6
5
4
3
2