doubt in array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • uppili4chi
    New Member
    • Oct 2007
    • 14

    doubt in array

    Good morning friends...

    is it possible to store one array to another array if it is like this


    s[][]=s1[];
    s[1][1]=s1[];

    what will happen for this code.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    As I'm sure weaknessforcats will explain in fuller detail, there are actually no two-dimensional arrays, or any other dimension arrays in C++. All these things are is single dimension arrays of arrays. For instance,

    [CODE=cpp]int matrix[5][5];[/CODE]

    Here you may recognize matrix as a 2-dimensional array. It is actually a 1 dimensional array, each element of which is a 1 dimensional array, each element of which is an int. So when you write

    [CODE=cpp]matrix[4][2] = 2;[/CODE]

    what are are saying is, "Access the 5th element of matrix (which is an array). Access the 3rd element of the resultant array (which is an int). Set this int to 2."

    With that in mind, I'm fairly certain you can set an array equal to another array. So, if you had matrix as above, and you had an array myArray:

    [CODE=cpp]int myArray[5] = {1, 2, 3, 4, 5};[/CODE]

    I think you can legally say:

    [CODE=cpp]matrix[1] = myArray;[/CODE]

    I'm not exactly sure, but why not go to a C++ compiler and try a simple program manipulating these things, and see what you get?

    Comment

    • RRick
      Recognized Expert Contributor
      • Feb 2007
      • 463

      #3
      Be careful when working with multi-dimensional arrays and arrays of pointers. They may look the same, but they don't work the same.

      In the above example, a 2D matrix was defined and then a row of its values were changed by another array. This isn't going to work. It would work if you were dealing with an array of pointers.
      [code=cpp]
      double arr[5] = { 1, 2, 3, 4, 5};
      double mat[5][5];
      double * matp[5];

      mat[1] = arr; // Won't work
      matp[1] = arr; // Works

      // Note how the syntax is the same
      mat[1][2] = 77;
      matp[1][2] = 77;
      [/code]
      With multi-dimensional arrays, the compiler does a lot of work that the user doesn't see. When accessing mat[1], the compiler will generate a temporary pointer to that location. With matp[1], it just returns the pointer. You can use those two pointers in the same way, but since the mat pointer is a temporary value, you can't assign it a value.

      Comment

      • uppili4chi
        New Member
        • Oct 2007
        • 14

        #4
        Originally posted by Ganon11
        With that in mind, I'm fairly certain you can set an array equal to another array. So, if you had matrix as above, and you had an array myArray:

        [CODE=cpp]int myArray[5] = {1, 2, 3, 4, 5};[/CODE]

        I think you can legally say:

        [CODE=cpp]matrix[1] = myArray;[/CODE]

        I'm not exactly sure, but why not go to a C++ compiler and try a simple program manipulating these things, and see what you get?
        sir i need to do in Java.... searching file concept i want to use the array and it complex less.

        if s is an array...
        s[5]=s is it possible ?

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by uppili4chi
          s[][]=s1[];
          s[1][1]=s1[];
          To start with, this won't compile. s[][] is invalid syntax in ANSI/ISO 1998 C++.

          Originally posted by uppili4chi
          f s is an array...
          s[5]=s is it possible ?
          Probably not. An array is not a type. s[5] is a type. You cannot assign a non-type to a type.

          Read this:
          Originally posted by weaknessforcats
          First, there are only one-dimensional arrays in C or C++. The number of elements in put between brackets:
          [code=c]
          int array[5];
          [/code]

          That is an array of 5 elements each of which is an int.

          [code=c]
          int array[];
          [/code]

          won't compile. You need to declare the number of elements.

          Second, this array:
          [code=c]
          int array[5][10];
          [/code]

          is still an array of 5 elements. Each element is an array of 10 int.

          [code=c]
          int array[5][10][15];
          [/code]

          is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.


          [code=c]
          int array[][10];
          [/code]

          won't compile. You need to declare the number of elements.

          Third, the name of an array is the address of element 0
          [code=c]
          int array[5];
          [/code]

          Here array is the address of array[0]. Since array[0] is an int, array is the address of an int. You can assign the name array to an int*.

          [code=c]
          int array[5][10];
          [/code]

          Here array is the address of array[0]. Since array[0] is an array of 10 int, array is the address of an array of 10 int. You can assign the name array to a pointer to an array of 10 int:
          [code=c]
          int array[5][10];

          int (*ptr)[10] = array;
          [/code]

          Fourth, when the number of elements is not known at compile time, you create the array dynamically:

          [code=c]
          int* array = new int[value];
          int (*ptr)[10] = new int[value][10];
          int (*ptr)[10][15] = new int[value][10][15];
          [/code]

          In each case value is the number of elements. Any other brackets only describe the elements.

          Using an int** for an array of arrays is incorrect and produces wrong answers using pointer arithmetic. The compiler knows this so it won't compile this code:

          [code=c]
          int** ptr = new int[value][10]; //ERROR
          [/code]

          new returns the address of an array of 10 int and that isn't the same as an int**.

          Likewise:
          [code=c]
          int*** ptr = new int[value][10][15]; //ERROR
          [/code]

          new returns the address of an array of 10 elements where each element is an array of 15 int and that isn't the same as an int***.

          Comment

          • uppili4chi
            New Member
            • Oct 2007
            • 14

            #6
            hi

            mr.weaknessforc ats i am very thank full to you.....great explanation...



            thank you very much

            Comment

            Working...