how to declare an array that takes any dimension specified by the user

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pallavi27
    New Member
    • Mar 2007
    • 14

    how to declare an array that takes any dimension specified by the user

    HI,
    i want to declare an array that should accept any size given by the user.how to declare such array?please explain me with the help of a code..it should display on the screen "enter the no of rows" and" enter the no of columns" and it should accept any size specified by the user.
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Hi,
    You cant declare dynamic array,but u can achieve it using pointers.
    Code:
    char **myArray;
    myArray=(char**)malloc(sizeof(char*)*(ROWSIZE));
    for(i=0;i<ROWSIZE;i++)
    {
         myArray[i]=(char*)malloc(COLSIZE);
    }
    Here ROWSIZE and COLSIZE are the inputs given by the user dynamically.

    Thanks
    Raghuram

    Comment

    • pallavi27
      New Member
      • Mar 2007
      • 14

      #3
      Originally posted by gpraghuram
      Hi,
      You cant declare dynamic array,but u can achieve it using pointers.
      Code:
      char **myArray;
      myArray=(char**)malloc(sizeof(char*)*(ROWSIZE));
      for(i=0;i<ROWSIZE;i++)
      {
           myArray[i]=(char*)malloc(COLSIZE);
      }
      Here ROWSIZE and COLSIZE are the inputs given by the user dynamically.

      Thanks
      Raghuram
      thankyou sir, but is it not at all possible to simple arrays to achieve that.i dont want to use pointers for this,isn't there any other way of doing it?

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        HI,
        Array size are needed during compile time, so u cant specify the size during run time.
        U can achieve this only using pointers in C.

        Thanks
        Raghuram

        Comment

        • pallavi27
          New Member
          • Mar 2007
          • 14

          #5
          Originally posted by gpraghuram
          HI,
          Array size are needed during compile time, so u cant specify the size during run time.
          U can achieve this only using pointers in C.

          Thanks
          Raghuram
          the array that i have taken is an image.the image i have saved using .TXT format and actually i have to read the image values and classify that and finally i have to store the result in another array.the image i have is 400 by 800.but my program to read the array should be generic so that if i have a bigger image to read, it shouldn't create any problem and accept the user desired value.one more thing that is i declare an array of size[120][120],it is taking,but not more than that.and if i declare size[10][2000],then it is accepting.why is it that if i decrease the value of row and increase the value of column it is taking?

          Comment

          • pallavi27
            New Member
            • Mar 2007
            • 14

            #6
            Originally posted by gpraghuram
            HI,
            Array size are needed during compile time, so u cant specify the size during run time.
            U can achieve this only using pointers in C.

            Thanks
            Raghuram
            the array that i have taken is an image.the image i have saved using .TXT format and actually i have to read the image values and classify that and finally i have to store the result in another array.the image i have is 400 by 800.but my program to read the array should be generic so that if i have a bigger image to read, it shouldn't create any problem and accept the user desired value.one more thing that is i declare an array of size[120][120],it is taking,but not more than that.and if i declare size[10][2000],then it is accepting.why is it that if i decrease the value of row and increase the value of column it is taking? please help me with this confusion.thank you.

            Comment

            • Indianraja
              New Member
              • Apr 2007
              • 22

              #7
              Originally posted by pallavi27
              the array that i have taken is an image.the image i have saved using .TXT format and actually i have to read the image values and classify that and finally i have to store the result in another array.the image i have is 400 by 800.but my program to read the array should be generic so that if i have a bigger image to read, it shouldn't create any problem and accept the user desired value.one more thing that is i declare an array of size[120][120],it is taking,but not more than that.and if i declare size[10][2000],then it is accepting.why is it that if i decrease the value of row and increase the value of column it is taking? please help me with this confusion.thank you.
              actually system doesnt knw abt ur size.... it only knw aboutsizes in bytes only. if u declare as like size[9][9] then we can store 9*9 values.... lik wis if u declare as lik [120][120] then may be it wil take much more spaces... but reason is this only......

              Comment

              Working...