1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

array in C

Discussion in 'C, C++, C#' started by Suppl, Aug 7, 2014.

  1. Suppl

    Suppl Newbie

    Joined:
    Nov 16, 2013
    Messages:
    16
    Likes Received:
    0
    I just have one question about preincrementation. For ex. we got
    Code:
    array[6] = { 1, 2, 3, 4, 5, 6 };
    for(i=0; i < 6; i++)
    {
    array[i]=array[i+1];
    }
    why this one does not work:
    Code:
    for(i=0; i < 6; 1)
    {
    array[i]=array[++i];
    }
     
  2. macdonjo3

    macdonjo3 Jr. VIP Jr. VIP Premium Member

    Joined:
    Nov 8, 2009
    Messages:
    5,582
    Likes Received:
    4,322
    Location:
    Toronto
    Home Page:
    I don't develop in C but the 3rd parameter doesn't change. It just loops at i=0
    You need to increment or decrement i with each loop iteration.
     
  3. eimFlood

    eimFlood Junior Member

    Joined:
    Dec 21, 2012
    Messages:
    172
    Likes Received:
    58
    It "works," just not the way you're expecting it to. The portion with the increment operator is evaluated first and it returns a value. The value will either be the value of 'i' before the incrementation or the value after the incrementation.

    In this case, you've chosen pre-incrementation, which increments the value of a variable and returns the resulting incremented value.

    So, using this line "array=array[++i]", the left side and right side are always accessing the same position in the array.

    If 'i' is 3:
    ++i increments i to 4, and then returns 4 so
    array[4]=array[4];

    If you were to use the post-increment operator, it would look like this:

    If 'i' is 3:
    i++ increments i to 4, and then returns 3 so
    array[4]=array[3];


    Notice that neither case will perform the same way as your initial code block that uses "array=array[i+1];".

    The functional equivalent of your initial code block would be this:

    array[6] = { 1, 2, 3, 4, 5, 6 };
    for(i=0; i < 6; 1)
    {
    array[i++]=array;
    }

    In that case, if 'i' is 3:
    ++i increments i to 4 and returns 3 so
    array[3]=array[4];
     
  4. eimFlood

    eimFlood Junior Member

    Joined:
    Dec 21, 2012
    Messages:
    172
    Likes Received:
    58
    He's incrementing from within the loop. It's perfectly fine and quite common.

    Note that you can easily turn a for-loop into a while-loop with for-loop syntax:

    for( ; i<6 ; )
    {
    i++;
    }

    is the same as

    while(i<6)
    {
    i++;
    }
     
  5. Suppl

    Suppl Newbie

    Joined:
    Nov 16, 2013
    Messages:
    16
    Likes Received:
    0
    eimFlood thanks(ofc other too) :)
     
  6. intruder2600

    intruder2600 Junior Member

    Joined:
    Jul 30, 2010
    Messages:
    160
    Likes Received:
    68
    I know Java basics, in this case it's similar with C, i think it's because this:
    (i=0; i < 6; 1)

    It can't be a static number. preincrement must be defined in for () as a third argument.
     
  7. eimFlood

    eimFlood Junior Member

    Joined:
    Dec 21, 2012
    Messages:
    172
    Likes Received:
    58
    Though it fails in Java, it's valid in GNU C.

    Give it a try:
    http://www.compileonline.com/compile_java_online.php
    http://www.compileonline.com/compile_c_online.php

    In fact, there's another important difference between Java and C here. Java will throw an "array out of bounds" exception, but the code will execute without complaints in C, even though the last iteration of the loop will set array[5] to an unknown value (that value being whatever happens to be in memory at the position of array+6 at the time).


    Also, note that if you wanted to do the incrementation inside the loop in Java, and you really wanted to use a for-loop, you could just construct the for-statement like this "for(i=0; i < 6; i=i)"
     
  8. wolverine

    wolverine Newbie

    Joined:
    Jul 7, 2008
    Messages:
    14
    Likes Received:
    0
    Problem is with the loop, you are not incrementing it right. make it like i++ and it will work properly
     
  9. member8200

    member8200 Regular Member

    Joined:
    Aug 9, 2014
    Messages:
    469
    Likes Received:
    33
    i think it's because the third parameter from the forloop is incorrect.
    the third parameter should be the incrementation. 'i++' . :)
     
    • Thanks Thanks x 1
  10. flyingwebtech

    flyingwebtech Newbie

    Joined:
    Jul 12, 2014
    Messages:
    20
    Likes Received:
    0
    Last element index will be out of bound so it wont work with your code a= a[i+1] or another one too
     
  11. selina88k

    selina88k Newbie

    Joined:
    May 12, 2015
    Messages:
    14
    Likes Received:
    0
    make it
    for(i=0; i < 6; i++)
    {
    array=array[++i];
    }
     
  12. bitm0de

    bitm0de Newbie

    Joined:
    Jun 13, 2015
    Messages:
    12
    Likes Received:
    1


    Actually you're wrong. The result of both:
    Code:
    array[i] = array[i++];
    And
    Code:
    array[i] = array[++i];
    Is undefined behavior. Look up sequence points. You shouldn't have code with either increment on i, and rather just i + 1...

    This would seem like a textbook reply to me. You don't know much about C...
     
    Last edited: Jun 13, 2015
  13. Miss Figa

    Miss Figa Newbie

    Joined:
    Aug 17, 2015
    Messages:
    12
    Likes Received:
    0
    Occupation:
    Women Clothing
    Location:
    United Kingdom
    Home Page:
    Array is a collection of variables belongings to the same data type. You can store group of data of same data type in an array.

    #include<stdio.h>
    int main()
    { int i;

    int arr[5] = {10,20,30,40,50};

    // declaring and Initializing array in C

    //To initialize all array elements to 0, use int arr[5]={0};

    /* Above array can be initialized as below also

    arr[0] = 10;
    arr[1] = 20;
    arr[2] = 30;
    arr[3] = 40;
    arr[4] = 50; */
    for (i=0;i<5;i++)
    {
    // Accessing each variable
    printf("value of arr[%d] is %d \n", i, arr);
    }
    }
     
  14. DigitalPigeon

    DigitalPigeon Newbie

    Joined:
    Sep 30, 2015
    Messages:
    16
    Likes Received:
    2
    Your first code will not work because at the end of the loop you are accessing an element outside the bounds of the array.

    The second code doesn't work because you are incrementing i with each step in the ++i call. So once again you are accessing elements outside the bounds of the array.
     
  15. DylanHoggarth

    DylanHoggarth Newbie

    Joined:
    Oct 22, 2015
    Messages:
    22
    Likes Received:
    12
    Just because you can not write, and it is not pretty. Shitcode is called.