數組一旦被定義了,它的維數和維界就不會變。因此,除了結構的初始化和銷毀之外,數組只有存取元素和修改元素。數組一般分為行序和列許。一般系統都是用的行許。
以2維數組a[m][n]為例
列序:
a[0][0] a[1][0] ...... a[m-1][0]
.............
a[m-1][0] a[m-1][1] ...... a[m-1][n-1]
行序列:
a[0][0] a[1][0] ...... a[m-1][0]
.............
a[0][n-1] a[1][ [n-1 ] ...... a[m-1][n-1]
數據一般使用連續的地址存儲。計算的方法為 (i, j) = &a + (b) + j;//b 一維元素的個數
推廣到多維數組中(j1, j2, j3,.....,jn) = (b2*b3*...*bn * j1 + b3*b4*...*bn*j2 + .....+bn *jn-1 + jn)
例如:已知數組a[5][4][10]。求a[2][3][4] 的地址;
j1=2、j2 = 3、j3 = 4;b1=5、b2=4、b3=10;
a[2][3][4]=b2*b3*j1+b3*j2+j3=4*10*2+10*3+4;
下面為代碼實現。(以下代碼是我在ubuntu中簡單測試通過。如在別的系統中無法運行,請諒解。本代碼只經過簡單的測試,如果出現問題,請諒解。)
[plain]
/*
*created by Reage at 2013 March 28
*description: 數組的實現,包含創建、賦值、訪問、打印
*
*blog:http://blog.csdn.net/rentiansheng
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#define MAXDIM 4
typedef struct Array
{
int dim;
int *ptr;
int *bounds;
int *base_add;
}Array;
int array_init(Array *a, int dim, ...);
int array_set_value(Array *a, int value, ...);
int array_print_line(Array *a);
int array_get_value(Array *a, ...);
void array_destory(Array *a);
int main(int argc, char *argv[])
{
Array a;
int i = 0;
int j;
int total = 1;
array_init(&a, 2, 4, 6);
for(; i < 4; i++)
{
for(j = 0; j < 6; j++)
{
array_set_value(&a, total++, i, j);
}
}
array_print_line(&a);
for(i = 0; i < 4; i++)
{
for(j = 0; j < 6; j++)
{
printf("%-7d",array_get_value(&a, i, j));
}
printf("\n");
}
array_destory(&a);
}
int array_init(Array * a, int dim, ...)
{
if(1 > dim || 8 < dim)
return -1;
a->dim = dim;
va_list ap;
int i;
long total = 1;
a->bounds = (int *)malloc(dim * sizeof(int));
va_start(ap, dim);
for(i = 0; i < dim; i++)
{
a->bounds[i] = va_arg(ap, int);
total *= a->bounds[i];
}
va_end(ap);
a->ptr = (int *) malloc(total * sizeof(int));
a->base_add = (int *) malloc(dim * sizeof(int));
a->base_add[dim -1] = 1;
i = dim -2;
for(; i >= 0; i--)
{
a->base_add[i] = a->base_add[i+1] * a->bounds[i+1];
}
return 0;
}
#define FREE(x) if(NULL != (x)) free(x)
void array_destory(Array *a)
{
FREE(a->ptr);
FREE(a->bounds);
FREE(a->base_add);
}
int array_get_value(Array *a, ...)
{
va_list va;
va_start(va, a);
int result = array_get_locate(a, va);
if(-1 == result) return -1;
return a->ptr[result];
}
int array_print_line(Array *a)
{
int total = 1;
int i = 0;
int line ;
for(; i < a->dim; i++)
{
total *= a->bounds[i];
}
line = total/a->bounds[0];
for(i = 0; i < total; i++)
{
if(0 == i % line && 0 != i) printf("\n");
printf("%-7d", a->ptr[i]);
}
printf("\n");
return 0;
}
int array_get_locate(Array *a, va_list va)
{
int result = 0;
int bound;
int i;
for(i = 0; i < a->dim; i++)
{
bound = va_arg(va, int);
if(0 > bound || bound > a->bounds[i])
{
return -1;
}
result += bound * a->base_add[i];
}
return result;
}
int array_set_value(Array *a, int value, ...)
{
if(NULL == a) return -1;
va_list va;
va_start(va, value);
int result = array_get_locate(a, va);
if( -1 == result) return -1;
a->ptr[result] = value;
return 0;
}
/*
*created by Reage at 2013 March 28
*description: 數組的實現,包含創建、賦值、訪問、打印
*
*blog:http://blog.csdn.net/rentiansheng
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#define MAXDIM 4
typedef struct Array
{
int dim;
int *ptr;
int *bounds;
int *base_add;
}Array;
int array_init(Array *a, int dim, ...);
int array_set_value(Array *a, int value, ...);
int array_print_line(Array *a);
int array_get_value(Array *a, ...);
void array_destory(Array *a);
int main(int argc, char *argv[])
{
Array a;
int i = 0;
int j;
int total = 1;
array_init(&a, 2, 4, 6);
for(; i < 4; i++)
{
for(j = 0; j < 6; j++)
{
array_set_value(&a, total++, i, j);
}
}
array_print_line(&a);
for(i = 0; i < 4; i++)
{
for(j = 0; j < 6; j++)
{
printf("%-7d",array_get_value(&a, i, j));
}
printf("\n");
}
array_destory(&a);
}
int array_init(Array * a, int dim, ...)
{
if(1 > dim || 8 < dim)
return -1;
a->dim = dim;
va_list ap;
int i;
long total = 1;
a->bounds = (int *)malloc(dim * sizeof(int));
va_start(ap, dim);
for(i = 0; i < dim; i++)
{
a->bounds[i] = va_arg(ap, int);
total *= a->bounds[i];
}
va_end(ap);
a->ptr = (int *) malloc(total * sizeof(int));
a->base_add = (int *) malloc(dim * sizeof(int));
a->base_add[dim -1] = 1;
i = dim -2;
for(; i >= 0; i--)
{
a->base_add[i] = a->base_add[i+1] * a->bounds[i+1];
}
return 0;
}
#define FREE(x) if(NULL != (x)) free(x)
void array_destory(Array *a)
{
FREE(a->ptr);
FREE(a->bounds);
FREE(a->base_add);
}
int array_get_value(Array *a, ...)
{
va_list va;
va_start(va, a);
int result = array_get_locate(a, va);
if(-1 == result) return -1;
return a->ptr[result];
}
int array_print_line(Array *a)
{
int total = 1;
int i = 0;
int line ;
for(; i < a->dim; i++)
{
total *= a->bounds[i];
}
line = total/a->bounds[0];
for(i = 0; i < total; i++)
{
if(0 == i % line && 0 != i) printf("\n");
printf("%-7d", a->ptr[i]);
}
printf("\n");
return 0;
}
int array_get_locate(Array *a, va_list va)
{
int result = 0;
int bound;
int i;
for(i = 0; i < a->dim; i++)
{
bound = va_arg(va, int);
if(0 > bound || bound > a->bounds[i])
{
return -1;
}
result += bound * a->base_add[i];
}
return result;
}
int array_set_value(Array *a, int value, ...)
{
if(NULL == a) return -1;
va_list va;
va_start(va, value);
int result = array_get_locate(a, va);
if( -1 == result) return -1;
a->ptr[result] = value;
return 0;
}