Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. passing 2 dim array

passing 2 dim array

Scheduled Pinned Locked Moved C / C++ / MFC
comdata-structuresquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    K Shaffer
    wrote on last edited by
    #1

    How can you reuse a function in which you want to pass a 2 dimentional array using different sizes? const int arr_size = 20; main { int 2_array[arr_size][arr_size]; .... print_arr(2_array); } //parameter must be passed like this void print_arr(int temp[][arr_size]) { ..... } Kevin Shaffer kshaff03@msn.com

    A J 2 Replies Last reply
    0
    • K K Shaffer

      How can you reuse a function in which you want to pass a 2 dimentional array using different sizes? const int arr_size = 20; main { int 2_array[arr_size][arr_size]; .... print_arr(2_array); } //parameter must be passed like this void print_arr(int temp[][arr_size]) { ..... } Kevin Shaffer kshaff03@msn.com

      A Offline
      A Offline
      Alexander M
      wrote on last edited by
      #2

      const int arr_size = 20;

      main
      {
      int 2_array[arr_size][arr_size];
      ....
      print_arr(&2_array);

      }

      //parameter must be passed like this
      void print_arr(int *temp)
      {
      .....
      }

      Don't try it, just do it! ;-)

      K 1 Reply Last reply
      0
      • A Alexander M

        const int arr_size = 20;

        main
        {
        int 2_array[arr_size][arr_size];
        ....
        print_arr(&2_array);

        }

        //parameter must be passed like this
        void print_arr(int *temp)
        {
        .....
        }

        Don't try it, just do it! ;-)

        K Offline
        K Offline
        K Shaffer
        wrote on last edited by
        #3

        When I try that, I get the following compilation error: function call print_arr'(int (*)[10][10])' does not match 'print_arr(int*)' error occurs when I call the function: print_arr(&2_array); //<------ Could it be my compiler? I have to use Metrowerks Codewarrior on this particular computer. Kevin Shaffer kshaff03@msn.com

        P 1 Reply Last reply
        0
        • K K Shaffer

          When I try that, I get the following compilation error: function call print_arr'(int (*)[10][10])' does not match 'print_arr(int*)' error occurs when I call the function: print_arr(&2_array); //<------ Could it be my compiler? I have to use Metrowerks Codewarrior on this particular computer. Kevin Shaffer kshaff03@msn.com

          P Offline
          P Offline
          Prakash Nadar
          wrote on last edited by
          #4

          kshaff03 wrote: print_arr(&2_array); //<------ change it to printf_arr((int*)&2_array); My God is more powerfull Than Your God.

          1 Reply Last reply
          0
          • K K Shaffer

            How can you reuse a function in which you want to pass a 2 dimentional array using different sizes? const int arr_size = 20; main { int 2_array[arr_size][arr_size]; .... print_arr(2_array); } //parameter must be passed like this void print_arr(int temp[][arr_size]) { ..... } Kevin Shaffer kshaff03@msn.com

            J Offline
            J Offline
            Jonas Larsson
            wrote on last edited by
            #5

            My corrections in bold **int** main**()** { // int 2_array[arr_size][arr_size]; // Illegal variable name. **int array_2[arr_size][arr_size];** print_arr(2_array); **return 0;** } Here's a small example program showing 2 diffrent versions of passing 2-dim arrays #include < iostream > using namespace std; const int arr_size = 5; // Should pass the upper limit to this function since it // cannot know how many elements there are in each dimension void print_arr(int *temp) { for (int i = 0;i < arr_size;++i) { cout << endl; for (int j = 0;j < arr_size; ++j) cout << temp[i*arr_size + j] << " "; } cout << endl; } // Same here for the number of rows void print_arr2(int temp[][arr_size]) { for (int i = 0;i < arr_size; ++i) { cout << endl; for (int j = 0;j < arr_size;++j) cout << temp[i][j] << " "; } cout << endl; } int main(int argc, char* argv[]) { int array2d[arr_size][arr_size] = {0}; print_arr(&array2d[0][0]); // get address of first element. No need for casts print_arr2(array2d); return 0; } HTH Jonas “Our solar system is Jupiter and a bunch of junk” - Charley Lineweaver 2002

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups