Arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • machakhama
    New Member
    • Jan 2014
    • 1

    Arrays

    how can shift element of array from one array to another
    eg
    Code:
    array1[] = {3,4,2,5}
    array2[] = {7,8,9,3,4}
    i want to shift fist element of array1 to the least element to array2
    e.g
    Code:
    array1[] = {4,2,5,7}
    array2[] = {8,9,3,4,3}
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This will be hard because you don't really have an array you can change.

    array1[] does not give you an array. What this is, is a pointer to a one dimension array. The {4,2,5,7} are integers that are constant. Also missing is the type of the pointer, like an int.

    Change the code to:

    Code:
    int array1[4] = {3,4,2,5}
    int array2[5] = {7,8,9,3,4}
    Now this defines array1 as an array of 4 ints with initial values of 3,4,2,5. Since this is your array, you are allowed t change the values of the elements.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      @weaknessforcat s: omitting the array size in an initializer is allowed in C (don't know about C++). In C, this means that the array size is implied -- it will match the initializer.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You're correct. I slipped up there. However you do need to have the array type:

        Code:
        int array1[] = {3,4,2,5};
        int array2[5] = {7,8,9,3,4};

        Comment

        Working...