constant multidimensional array in a function argument

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Szabolcs Borsanyi

    constant multidimensional array in a function argument

    The following code compiles with a warning message:
    passing arg 1 of `use_vector' from incompatible pointer type

    How can one correctly pass a multidimensiona l array (by reference)
    expressing that 'use_vector'
    is not meant to modify any of the elements.

    Thanks
    Szabolcs

    ------------------------------------------------------------
    enum { E=3 };
    typedef double vector[4];

    void use_vector(cons t vector x[E])
    {
    }

    int main()
    {
    vector v[E]={0};
    use_vector(v);
    }
  • Ben Bacarisse

    #2
    Re: constant multidimensiona l array in a function argument

    Szabolcs Borsanyi <borsanyi@thphy s.uni-heidelberg.dewr ites:
    The following code compiles with a warning message:
    passing arg 1 of `use_vector' from incompatible pointer type
    Yes, you have hit a small hole in the way C handles qualified types.
    How can one correctly pass a multidimensiona l array (by reference)
    expressing that 'use_vector'
    is not meant to modify any of the elements.
    Pretty much all you can do is use a cast:

    use_vector((con st vector *)v);

    (or drop the const from the parameter).
    enum { E=3 };
    typedef double vector[4];
    >
    void use_vector(cons t vector x[E])
    {
    }
    >
    int main()
    {
    vector v[E]={0};
    use_vector(v);
    }
    --
    Ben.

    Comment

    Working...