开发者

invalid use of array with unspecified bounds

i had a question in my program. When I pass the 3D int array CodedGreen to the function Green_Decode_Tree. An error message"invalid use of array with unspecified bounds" displayed. What is the mistake in my program? Thanks for your help.

for(i=0;i<256;i++){
          for(j=0;j<256;j++){
    Decode_Tree(green[0], CodedGreen,0,i,j);
          }
      }

vo开发者_JAVA技巧id Green_Decode_Tree(node* tree, int code[][][], int num,int row,int col)
{
    int i;
    i=num;

    if((tree->left == NULL) && (tree->right == NULL)){
        fprintf(DecodGreen,"%s\n", tree->ch);
    }
    else
    {
        if(code[row][col][num]==1){
            i++;
            Green_Decode_Tree(tree->left,code,i,row,col);
        }
        else if (code[row][col][num]==0){
            i++;
            Green_Decode_Tree(tree->right,code,i,row,col);
        }

    }

}


i will reveal you a secret. 2d (and 3d) arrays are represented as liner memory arrays. when you have array NxM and access it like a[i][j] it is actually translated to a[i*M + j] as you might notice compiler must know M here to do this conversion, otherwise it will not be able to translate it. So thats what he asks. You must provide all except first sizes in array: int code[][M][N]


Remember that in most contexts, array expressions have their types implicitly converted ("decay") from "N-element array of T" to "pointer to T" and evaluate to the address of the first element. When you pass CodedGreen (type int [X][Y][Z]) to Green_Decode_Tree, what the function receives is a pointer value of type int (*)[Y][Z].

So your prototype for Green_Decode_Tree needs to be

void Green_Decode_Tree(node *tree, int (*code)[Y][Z], int num, int row, int col)

Note that in the context of a function parameter declaration, int *a is synonymous with int a[] (no size), so int (*code)[Y][Z] could also be written as int code[][Y][Z]. I prefer using pointer notation, since that's what the function actually receives, but either will work. Note that in your function you will subscript it as normal:

if (code[row][num][col] == 1)

since the subscript operator implicitly dereferences the pointer (i.e., code[row] == *(code+row)).

This may be helpful:

Declaration         Expression        Type            Decays to
-----------         ----------        ----            ---------
T a[X];                      a        T [X]           T *
                            &a        T (*)[X]   

T b[X][Y];                   b        T [X][Y]        T (*)[Y]
                            &b        T (*)[X][Y]
                          b[i]        T [Y]           T *
                         &b[i]        T (*)[Y]

T c[X][Y][Z];                c        T [X][Y][Z]     T (*)[Y][Z]
                            &c        T (*)[X][Y][Z]  
                          c[i]        T [Y][Z]        T (*)[Z]
                         &c[i]        T (*)[Y][Z]
                       c[i][j]        T [Z]           T *

The expressions a, b, b[i], c, c[i], and c[i][j] are all array expressions, so their types will decay to pointer types in most contexts. The exceptions are when the array expressions are operands of the sizeof or address-of & operators (as is shown in the table), or when the array expression is a string literal being used to initialize another array in a declaration.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜