Thread: Returning Multidimensional arrays.

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    40

    Returning Multidimensional arrays.

    In my program i want to return a 3D array,(or an array of arrays of arrays) but I keep getting an error that i dont quite understand.

    Code:
    int*** create_board(int width, int height)
    {
        int board[width][height][9];
        
        for(int i=0; i<width; i++)
            for(int j =0; j<height; j++)
                for(int k=0; k<9; k++)
                    board[i][j][k] = 1;
        return board;
    }
    it says there is a problem with the line "return board;"

    main.cpp:38: error: cannot convert ‘int (*)[(((long unsigned int)(((long int)height) + -0x00000000000000001)) + 1)][9]’ to ‘int***’ in return

  2. #2
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Well the first thing is that you are creating your board on the stack using an automatic variable which will destruct when it falls out of scope on return. IF you are going to do this, you need to allocate it with malloc or new (forgot which forum I am in). Secondly, you are not really returning a "pointer to a pointer to a pointer" here, are you? You are returning a pointer to a block of memory and it is all in how it is accessed whether it acts like a 3D array or not...

    Caveat: I have taken my arthritis meds so the above may be FOS. ^__^

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    40
    ok, its been a while since i've used c++, they use python in school... nested lists are alot easier than multidimensional arrays...

    i'll be accessing it like this

    board[x][y][attrib]...

    so if i did int board = new int[width][height][9]; that would make it in the heap? so it wouldn't be destroyed when it goes out of scope? (I just tried it and it says i cant use variables(width, height) in a constant expression)

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    40
    thanks! Its alot more involved than i thought it would be.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    If you really want to make your life easier, just use the STL:

    Code:
    
    
    #include <vector>
    
    typedef std::vector< int >
    	dim3;
    typedef std::vector< dim3 >
    	dim2;
    typedef std::vector< dim2 >
    	board;
    	
    board create_board( int width, int height )
    {
    	return board( width, dim2( height, dim3( 9, 1 ) ) );
    }
    Also, unlike the dynamic-allocation approach (which violates RAII), there's no chance of a memory leak.

  6. #6
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    you are also attempting to return a pointer to a temporary object.

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    1
    Hi all, I have some snippet of code which is to take simbol by simbol from the file c:\\mnrf2.txt and set them into the array x[i2]. Instead of returning single simbol it returns two ones, could anyone help me how to do that it returns only one symbol. Data in the file:
    64208400
    76307712
    64201912
    63867925
    64201730
    63208159
    64208063
    63201464


    this is the code:
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    #include <stdio.h>
    
    
    
    
    
    
    int input;
    int neu;
    float m[100][100][100];
    ifstream in;
    ofstream out;
    int elem;
    int i2;
    char *x[100];
    float p[100][100];
    int it;
    double ran_n;
    double min_p;
    char ch[8];
    unsigned char *split[40];
    
    double  t_constant;
    double Radius1;
    double Radius;
    double xmin;
    double xmax;
    double ymin;
    double ymax;
    
    double L0;
    double L;
    double Teta;
    double m_d_radius;
    int jw;
    int kw;
    float circle_res;
    
    float xx;
    
    
    float assign_random  ()
    {   float tt =(rand())/((float)RAND_MAX+1);
    return ( tt);}
    
    
     float circle (int num1,int kw,int num2,int jw)
    
     {
     float circle_change=sqrt((num1-kw)*(num1-kw)+(num2-jw)*(num2-jw));
    
     return(circle_change);}
    
    
    
    
    double sqr2(double number)
    { double p=number;
    
    return(p*p);}
    
    
    float dis (float y2, float y1,int stepen)
    {   long double  p=y2-y1;
    for (int u=1;u<=stepen;u++)
    {p=p*2/100;}
    return(p);}
    
    
    float w_next (float w0,float xi,float pjk,float Radius2,float L)
    {
     float Teta=exp(-(pjk/(2*Radius2*Radius2)));
    float w= w0+L*Teta*(w0-xi);
    
    return(w);
    }
    
    
    
    
    int massive (char *array, int num_elements)
    {
      for (int q=1;q<num_elements;q++)
      {
      array++;
      }
      return(*array);
      }
    
    
    
    
     void main()
     {
    
    input=8;
    neu=5;
     it=5;
      //L0=StrTodouble(Edit4->Text);
        L0=0.2;
     m_d_radius=neu/2;
    
     t_constant= it/(log10( m_d_radius));
    
    
    
    
    
    
    
        //initialization of inputs
    
    for (int j=1;j<=neu;j++)
    {
    for (int k=1;k<=neu;k++)
    {
    
    
    for (int i=1;i<=input;i++)
    {
    
    
    
    m[i][j][k]= assign_random();
    
      cout<<"m[i][j][k] = "<<m[i][j][k]<<  '\n';
    
    }}}
    /////////////////////////////////////
    
    
    ///////cycle of iterations ////////////////////
    
    for (int b=1;b<= it;b++)
    {
     Radius1= m_d_radius*exp(-(b/ t_constant));
       L=L0*exp(-(b/ t_constant));
      Radius=floor(Radius1);
    
         out<<"radius = "<<Radius1<< '\t'<<Radius<< '\n';
    
         i2=1;
    
        in.open("c:\\mnrf2.txt");
            //out.open("c:\\vectors.txt");
        while (!in.eof())
    
        {
    
    
           in.getline(ch,8);
    
          x[1]=strtok(ch,"");
                 cout<<*x[1]<<'\n';
    
                  break;
     //for (int i=2;i<=input;i++)
    // {
    
    
    
    
      // x[i]=strtok(NULL,"");
    
        //  if(x[i]==NULL)
       //  { break;}
      // cout<<*x[i]<<'\t';
      // }
      // cout<<'\n';
    
    
    
    
    
    
    
    
    
           //to remove then cure bracket
       // for (int j=1;j<=neu;j++)
    //{
    //for (int k=1;k<=neu;k++)
    //{
    
        //   p[j][k]=0;
    //for (int i=1;i<=input;i++)
    //{
    
    
      //  p[j][k]=p[j][k]+dis(x[i],m[i][j][k],input-i);
    
    
       //   }
     //
    
     //   }
    
      //  }
    
     //    min_p=p[1][1];
      //   jw=1;
     //    kw=1;
    
    //   for (int j=1;j<=neu;j++)
     //  {
     //  for (int k=1;k<=neu;k++)
    //{
    
    //   if (p[j][k]<min_p)
     //  {min_p=p[j][k];
      // jw=j;
     //  kw=k;  }
    
    //   }
     //  }
    
      // printf("winner = %f",min_p,'\t',"jw=",jw,'\t',"kw=",kw,'\n');
    
    
     //Radius defining///////////////////
    
    //  xmin=kw- Radius;
    //  xmax=kw+Radius;
     // ymin=jw-Radius;
     // ymax=jw+Radius;
    
     // for (int j=ymin;j<=ymax;j++)
     // {
    //  for (int k=xmin;k<=xmax;k++)
    //  {
     // circle_res=circle(k,kw,j,jw);
    
    
    //  if (circle_res<= Radius)
    
     // {
      //    for (int i=1;i<=input;i++)
      //    {
    
    
      //  m[i][j][k]= w_next ( m[i][j][k],x[i],p[j][k],Radius,L);
    
    
        //m[i][j][k]= m[i][j][k]+L*Teta*(m[i][j][k]-x[i]);
      //  }
    
    
    
      //  }
    //    }
     //   }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
     //  }
    
      //    }
    
    
    
        i2++;
       }
    
    
     in.close();
    
    
        }
    
    
    
      //out.close();
                     }

  8. #8
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Holy unformatted code Batman~!!!
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Arrays in a function prototype
    By Enanito01478 in forum C Programming
    Replies: 2
    Last Post: 10-11-2009, 01:10 AM
  2. Replies: 0
    Last Post: 05-29-2009, 05:48 AM
  3. Multidimensional Arrays
    By jordanguyoflove in forum C Programming
    Replies: 4
    Last Post: 10-16-2008, 06:16 PM
  4. Multidimensional arrays returning.
    By eXeCuTeR in forum C Programming
    Replies: 33
    Last Post: 07-01-2008, 12:27 PM
  5. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM