下面的代码有一个名为ReadOnly[]
的数组,其中包含指向其他数组(如AV_ReadOnly
、BV_ReadOnly
等)的指针的元素。同样AV_ReadOnly
、BV_ReadOnly
等是指针数组,包含指向整数数组的元素。
read_arrays()
是用于打印特定列表/访问整数数组的任何特定值的函数。此方法在测试环境中非常有效。 但是,随着平台/编译器的更改,这种方法是否有可能失败?
#include<stdio.h>
int AV1_ReadOnly[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int AV2_ReadOnly[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
int BV1_ReadOnly[] = { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
int BV2_ReadOnly[] = { 31, 32, 33, 34, 35, 36, 37, 38, 39, 40};
int MV1_ReadOnly[] = { 41, 42, 43, 44, 45, 46, 47, 48, 49, 50};
int MV2_ReadOnly[] = { 51, 52, 53, 54, 55, 56, 57, 58, 59, 60};
int NC1_ReadOnly[] = { 61, 62, 63, 64, 65, 66, 67, 68, 69, 70};
int NC2_ReadOnly[] = { 71, 72, 73, 74, 75, 76, 77, 78, 79, 80};
int * AV_ReadOnly[] =
{
AV1_ReadOnly,
AV2_ReadOnly,
};
int * BV_ReadOnly[] =
{
BV1_ReadOnly,
BV2_ReadOnly,
};
int * MV_ReadOnly[] =
{
MV1_ReadOnly,
MV2_ReadOnly,
};
int * NC_ReadOnly[] =
{
NC1_ReadOnly,
NC2_ReadOnly
};
int ** ReadOnly[] =
{
AV_ReadOnly,
BV_ReadOnly,
MV_ReadOnly,
NC_ReadOnly
};
void read_arrays( int obj, int inst )
{
int ** ArrayPtr = ReadOnly[obj];
int count =0;
while( count <8 )
{
printf( "n %d", *(ArrayPtr[inst]+count) );
count++;
}
}
void main()
{
read_arrays( 1,1 );
}
只要您将int
数组和int*
数组保留在同一文件中,就可以了。
此外,如果您无意更改它们,您可以(应该)const
声明它们。
此外,如果您无意在其他文件中extern
它们,您可以(应该)static
声明它们。
顺便说一句,从您发布的内容来看,您似乎可以简单地使用int table[8][10]
......