One and two - dimensional array passing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • VanKha
    New Member
    • May 2007
    • 21

    One and two - dimensional array passing

    If I have an array int a[10] (just for example) , I can pass the array to a function void myf ( int * x) with the call myf ( a ) . But with the 2-dimensional array int a[10][10] , using myf ( int ** x ) ( or ( int a[][]) with the call myf ( a ) will generate an error (unrelated pointed types , etc..) . Why ?
  • VanKha
    New Member
    • May 2007
    • 21

    #2
    Please help me . Thanks a lot.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by VanKha
      Please help me . Thanks a lot.
      Note that a two dimensional array is just a one dimensional array where each of
      its elements happen to be one dimensional arrays as well.

      Similar to passing a one dimensional array 'a', where the parameter 'a' is considered
      a pointer to its first element, as in:

      [code=c]
      char array[42]; // first element is a char
      f(a);
      ...
      void f(char* a) { ... }
      [/code]

      so is the two dimensional array considered a pointer to its first element:

      [code=c]
      char array[42][54]; // first element is a char[54]
      f(a);
      ...
      void f(char *a[54]) { ... }
      [/code]

      kind regards,

      Jos

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Not correct:
        Originally posted by JosAH
        char array[42][54]; // first element is a char[54]
        f(a);
        ...
        void f(char *a[54]) { ... }
        This shows the argument to be an array of 54 character pointers. What is needed is a pointer to an array of 54 characters:

        [code=cpp
        char array[42][54]; // first element is a char[54]
        f(a);
        ...
        void f(char (*a)[54]) { ... }
        [/code]

        Comment

        Working...