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. *** Declaring Array inside another Array ***

*** Declaring Array inside another Array ***

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structurestutorial
3 Posts 3 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.
  • S Offline
    S Offline
    Steve Lai
    wrote on last edited by
    #1

    Hi Everyone, How do I declare an Array inside another Array? For example, I have the following two arrays: ///////////////////////////////////////////////////////// int UserInputSize1, UserInputSize2; int *array1; array1= new int[UserInputSize1]; int *array2; array2=new int[UserInputSize2]; //the size of the arrays will be determined by the user input during runtime. /////////////////////////////////////////////////////// How do I declare them so under array1, it’ll contain the other array? So for example, if the size of array1 is 3 and the size of array2 is 10, then under every element in array1, I’ll be able to store 10 numbers. Then I’ll be able to store a total of 30 numbers with this example. I would like to do something like this: Array1[0].Array2[0]=5; Array1[0].Array2[1]=10; Array1[0].Array2[3]=11; … If anyone knows, Please let me know. Thanks in advance Steve

    M S 2 Replies Last reply
    0
    • S Steve Lai

      Hi Everyone, How do I declare an Array inside another Array? For example, I have the following two arrays: ///////////////////////////////////////////////////////// int UserInputSize1, UserInputSize2; int *array1; array1= new int[UserInputSize1]; int *array2; array2=new int[UserInputSize2]; //the size of the arrays will be determined by the user input during runtime. /////////////////////////////////////////////////////// How do I declare them so under array1, it’ll contain the other array? So for example, if the size of array1 is 3 and the size of array2 is 10, then under every element in array1, I’ll be able to store 10 numbers. Then I’ll be able to store a total of 30 numbers with this example. I would like to do something like this: Array1[0].Array2[0]=5; Array1[0].Array2[1]=10; Array1[0].Array2[3]=11; … If anyone knows, Please let me know. Thanks in advance Steve

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      You first make an array of int* (in effect, an array of arrays) and then allocate one array for each of the int pointers. Thus:

      int** bigarray = new int* [UserInputSize1];

      for (int i=0; i < UserInputSize1; i++)
      bigarray[i] = new int [UserInputSize2];

      bigarray[0][0] = 5;
      bigarray[0][1] = 10;
      bigarray[0][2] = 15;

      Then free the memory in reverse order:

      for (int i=0; i < UserInputSize1; i++)
      delete [] bigarray[i];

      delete [] bigarray;

      1 Reply Last reply
      0
      • S Steve Lai

        Hi Everyone, How do I declare an Array inside another Array? For example, I have the following two arrays: ///////////////////////////////////////////////////////// int UserInputSize1, UserInputSize2; int *array1; array1= new int[UserInputSize1]; int *array2; array2=new int[UserInputSize2]; //the size of the arrays will be determined by the user input during runtime. /////////////////////////////////////////////////////// How do I declare them so under array1, it’ll contain the other array? So for example, if the size of array1 is 3 and the size of array2 is 10, then under every element in array1, I’ll be able to store 10 numbers. Then I’ll be able to store a total of 30 numbers with this example. I would like to do something like this: Array1[0].Array2[0]=5; Array1[0].Array2[1]=10; Array1[0].Array2[3]=11; … If anyone knows, Please let me know. Thanks in advance Steve

        S Offline
        S Offline
        Sam Hobbs
        wrote on last edited by
        #3

        The following is a complete program. Is it what you are asking for? If so, then there are many good books about the C and C++ languages that would cover such things much better than the volunteers helping with this forum. #include using namespace std; int main(int argc, char *argv[], char *envp[]) { int Array[3][10], i=0, i1, i2; for (i1=0; i1<3; ++i1) for (i2=0; i2<10; ++i2) Array[i1][i2] = i++; for (i1=0; i1<3; ++i1) { for (i2=0; i2<10; ++i2) cout << Array[i1][i2] << ' '; cout << endl; } return 0; }

        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