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. Return a local 2d Array

Return a local 2d Array

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorialquestion
6 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
    kinderu
    wrote on last edited by
    #1

    In the following code, I tried to return a 2D local array from the getArray function and display it in the main function. I use the following line to display the matrix

    cout << " " << *(*(ptr+i*COL)+j)

    but it is not displayed. Does anyone know how to display the array?

    #include
    #define ROW 3
    #define COL 4
    using namespace std;

    int main()
    {
    int **ptr;
    int **getArray();

    ptr = getArray();
    cout << "\\n Array tab\[\]\[\] in main(): " << endl << endl;
    for (int i=0; i
    
    L J 2 Replies Last reply
    0
    • K kinderu

      In the following code, I tried to return a 2D local array from the getArray function and display it in the main function. I use the following line to display the matrix

      cout << " " << *(*(ptr+i*COL)+j)

      but it is not displayed. Does anyone know how to display the array?

      #include
      #define ROW 3
      #define COL 4
      using namespace std;

      int main()
      {
      int **ptr;
      int **getArray();

      ptr = getArray();
      cout << "\\n Array tab\[\]\[\] in main(): " << endl << endl;
      for (int i=0; i
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Try this:

          for (int j=0; j
      
      K 1 Reply Last reply
      0
      • L Lost User

        Try this:

            for (int j=0; j
        
        K Offline
        K Offline
        kinderu
        wrote on last edited by
        #3

        Why this line? cout << " " << (int)*(ptr+i+j) << endl;

        Can you give me more details ? Why do you need a type-casting conversion ?

        L L 2 Replies Last reply
        0
        • K kinderu

          Why this line? cout << " " << (int)*(ptr+i+j) << endl;

          Can you give me more details ? Why do you need a type-casting conversion ?

          L Offline
          L Offline
          leon de boer
          wrote on last edited by
          #4

          Multidimensional arrays declared the way you have are just an abstraction for programmers, since the same results can be achieved with a simple array, by multiplying its indices: int jimmy [3][5]; // is equivalent to int jimmy [15]; // (3 * 5 = 15) In effect GetArray returns a 1D pointer array and you could have simply returned it as int* as it's really a big 1 dimensional array You returned int** so the typecast is to deal with it but it would have been simpler like this

          int *getArray()
          {
          static int tab[ROW][COL] = {
          11, 12, 13, 14,
          21, 22, 23, 24,
          31, 32, 33, 34,
          };
          return (int*)tab;
          }

          Then the access becomes more obvious

          int *ptr = getArray();
          cout << "\n Array tab[][] in main(): " << endl << endl;
          for (int i = 0; i
          In vino veritas

          1 Reply Last reply
          0
          • K kinderu

            Why this line? cout << " " << (int)*(ptr+i+j) << endl;

            Can you give me more details ? Why do you need a type-casting conversion ?

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            For some reason, cout was printing the numbers in hex rather than decimal.

            1 Reply Last reply
            0
            • K kinderu

              In the following code, I tried to return a 2D local array from the getArray function and display it in the main function. I use the following line to display the matrix

              cout << " " << *(*(ptr+i*COL)+j)

              but it is not displayed. Does anyone know how to display the array?

              #include
              #define ROW 3
              #define COL 4
              using namespace std;

              int main()
              {
              int **ptr;
              int **getArray();

              ptr = getArray();
              cout << "\\n Array tab\[\]\[\] in main(): " << endl << endl;
              for (int i=0; i
              
              J Offline
              J Offline
              jfbode1029
              wrote on last edited by
              #6

              The return type for getArray needs to be int (*getArray())[COL];. An expression of type T [M][N] decays to type T (*)[N], not T **. So your code needs to be

              int main()
              {
              int (*ptr)[N];
              int (*getArray())[N];
              ...
              }

              int (*getArray())[N]
              {
              ...
              return tab;
              }

              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