This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

2 dimentional arrays

Has anyone found a way to pass 2 dimentional arrays to a function? My arrays are stored in code and I need to pass them to a function to display them.

Gary Martin

Parents
  • Yes,

    Without testing, it works. But when tested it doesn't.

    void func(int ** pArray2d)
    

    is a function that expects pArray2d to be a pointer to a pointer to an int. This is NOT what is needed.

    This is one of the areas of C that most confuses people. In most contexts, the compiler converts a reference to an array into a pointer to the first element of the array. This leads people to think that arrays and pointers are equivalent. They aren't.

    In C, multi-dimensional arrays are implemented as arrays of arrays. So the first element of a two-dimensional array is a one-dimensional array. Therefore, a pointer to the first element of a two-dimensional array is a pointer to a one-dimensional array - not a pointer to a pointer.

    Here is a working example:

    unsigned char _2d_array [5][5];
    
    void func (unsigned char (*x)[5])
    {
    x [0][0] = 1;
    x [1][0] = 6;
    x [2][1] = 12;
    }
    
    void main (void)
    {
    func (_2d_array);
    
    while (1);
    }
    

    Note that func:

    void func (unsigned char (*x)[5])
    

    is declared with an argument that is a pointer to an array of 5 items. This is required so that the compiler knows how to handle the multiple dimensions.

    The parentheses around *x are required. We want x to be a pointer to an array of 5 unsigned chars. Without the parentheses, the '[]' would take precedence over the '*', and x would be an array of 5 pointers to unsigned char.

    Hope this helps.

    Keil Support


Reply
  • Yes,

    Without testing, it works. But when tested it doesn't.

    void func(int ** pArray2d)
    

    is a function that expects pArray2d to be a pointer to a pointer to an int. This is NOT what is needed.

    This is one of the areas of C that most confuses people. In most contexts, the compiler converts a reference to an array into a pointer to the first element of the array. This leads people to think that arrays and pointers are equivalent. They aren't.

    In C, multi-dimensional arrays are implemented as arrays of arrays. So the first element of a two-dimensional array is a one-dimensional array. Therefore, a pointer to the first element of a two-dimensional array is a pointer to a one-dimensional array - not a pointer to a pointer.

    Here is a working example:

    unsigned char _2d_array [5][5];
    
    void func (unsigned char (*x)[5])
    {
    x [0][0] = 1;
    x [1][0] = 6;
    x [2][1] = 12;
    }
    
    void main (void)
    {
    func (_2d_array);
    
    while (1);
    }
    

    Note that func:

    void func (unsigned char (*x)[5])
    

    is declared with an argument that is a pointer to an array of 5 items. This is required so that the compiler knows how to handle the multiple dimensions.

    The parentheses around *x are required. We want x to be a pointer to an array of 5 unsigned chars. Without the parentheses, the '[]' would take precedence over the '*', and x would be an array of 5 pointers to unsigned char.

    Hope this helps.

    Keil Support


Children