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. How to return 2-dimension array in a function

How to return 2-dimension array in a function

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelptutorial
7 Posts 5 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.
  • A Offline
    A Offline
    akira32
    wrote on last edited by
    #1

    In getA, I can get 1-dimension array. But in getB, it will appear the error message as below: Error 2 error C2440: 'return' : cannot convert from 'int [3][2]' to 'int **'

    int* getA()
    {
    int a[2];
    return a;
    }

    int** getB()
    {
    int b[3][2];

    return b;
    

    }

    CPalliniC A L S 4 Replies Last reply
    0
    • A akira32

      In getA, I can get 1-dimension array. But in getB, it will appear the error message as below: Error 2 error C2440: 'return' : cannot convert from 'int [3][2]' to 'int **'

      int* getA()
      {
      int a[2];
      return a;
      }

      int** getB()
      {
      int b[3][2];

      return b;
      

      }

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      Why bother? Returning a temporary would trouble you anyway. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • A akira32

        In getA, I can get 1-dimension array. But in getB, it will appear the error message as below: Error 2 error C2440: 'return' : cannot convert from 'int [3][2]' to 'int **'

        int* getA()
        {
        int a[2];
        return a;
        }

        int** getB()
        {
        int b[3][2];

        return b;
        

        }

        A Offline
        A Offline
        Aescleal
        wrote on last edited by
        #3

        akira32 wrote:

        In getA, I can get 1-dimension array.

        The first thing here is that you're not "get[ing] 1-dimension array." you're returning the address of the first element of where a 1 dimensional array was before the function returned. Depending on what you do with the pointer after the function returns you could crash your program or even leave it with a big security hole. The bad news is that you can't return an array of any sort directly from a function. The slightly better news is that you can return a structure containing an array, e.g:

        struct array_wrapper
        {
        int data[10];
        };

        struct array_wrapper get_array()
        {
        struct array_wrapper a;
        a.data[5] = 100;
        return a;
        }

        Quick caveat: I don't program in C anymore so I may have got the syntax a bit wrong. Cheers, Ash Edited to fix slightly knackered code formatting

        modified on Friday, May 14, 2010 5:54 PM

        1 Reply Last reply
        0
        • A akira32

          In getA, I can get 1-dimension array. But in getB, it will appear the error message as below: Error 2 error C2440: 'return' : cannot convert from 'int [3][2]' to 'int **'

          int* getA()
          {
          int a[2];
          return a;
          }

          int** getB()
          {
          int b[3][2];

          return b;
          

          }

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

          Both these calls leave you with memory leaks. If you want to allocate arrays to be returned from a function you should use one of the memory allocation functions, malloc() or new.

          It's time for a new signature.

          A 1 Reply Last reply
          0
          • A akira32

            In getA, I can get 1-dimension array. But in getB, it will appear the error message as below: Error 2 error C2440: 'return' : cannot convert from 'int [3][2]' to 'int **'

            int* getA()
            {
            int a[2];
            return a;
            }

            int** getB()
            {
            int b[3][2];

            return b;
            

            }

            S Offline
            S Offline
            sashoalm
            wrote on last edited by
            #5

            You shouldn't be returning an array, the best thing is to accept a pointer to an array allocated by the caller (plus dimensions) and then fill it. That way you'll avoid the problem of who has to do the cleanup of the memory. For example this function will fill the array with random values:

            int foo (int* dstArr, int m, int n)
            {
            for (int i=0;i
            There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition.
            Blaise Pascal

            A 1 Reply Last reply
            0
            • L Lost User

              Both these calls leave you with memory leaks. If you want to allocate arrays to be returned from a function you should use one of the memory allocation functions, malloc() or new.

              It's time for a new signature.

              A Offline
              A Offline
              Aescleal
              wrote on last edited by
              #6

              Hi, Sorry to sound picky but it's important for C programmers to understand these things... The original functions the original author posted didn't leak anything (well, the one that would have compiled anyway) - all the memory allocated for his arrays on the stack were cleaned up when the functions returned. The problem he would have caused was actually the reverse of a leak: continuing to access memory that's been marked as inaccessible for some reason. This is usually called a "dangling pointer" as it's pointing somewhere it shouldn't. In this case it's pointing to a block of memory on the stack and writing to it could cause all sorts of fun and merriment. Cheers, Ash

              1 Reply Last reply
              0
              • S sashoalm

                You shouldn't be returning an array, the best thing is to accept a pointer to an array allocated by the caller (plus dimensions) and then fill it. That way you'll avoid the problem of who has to do the cleanup of the memory. For example this function will fill the array with random values:

                int foo (int* dstArr, int m, int n)
                {
                for (int i=0;i
                There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition.
                Blaise Pascal

                A Offline
                A Offline
                akira32
                wrote on last edited by
                #7

                good,thank you all

                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