increment in if statement

Discussion in 'C Programming' started by Nishu, Mar 1, 2007.

  1. Nishu

    Nishu Guest

    Hi All,

    My Aim here is to increment lArray whether control goes inside if
    statement or not. Is this a portable step and C guarantees that lArray
    must be incremented?

    Thanks,
    Nishu


    #include<stdio.h>

    int main(void)
    {
    int i = 7;
    int array[] = {0,1,1,0,1,1,0};
    int* lArray;

    lArray = array;

    while(i--)
    {
    if(*lArray++) /* Is this a portable step? */
    {
    printf("enters in if loop\n");
    }
    }
    return 0;

    }
     
    Nishu, Mar 1, 2007
    #1
    1. Advertising

  2. Nishu

    Danny Guest

    On Mar 1, 9:50 am, "Nishu" <> wrote:
    > Hi All,
    >
    > My Aim here is to increment lArray whether control goes inside if
    > statement or not. Is this a portable step and C guarantees that lArray
    > must be incremented?
    >
    > Thanks,
    > Nishu
    >
    > #include<stdio.h>
    >
    > int main(void)
    > {
    > int i = 7;
    > int array[] = {0,1,1,0,1,1,0};
    > int* lArray;
    >
    > lArray = array;
    >
    > while(i--)
    > {
    > if(*lArray++) /* Is this a portable step? */
    > {
    > printf("enters in if loop\n");
    > }
    > }
    > return 0;
    >
    > }


    Yes. As far as I know, you're safe.
     
    Danny, Mar 1, 2007
    #2
    1. Advertising

  3. Nishu

    Flash Gordon Guest

    Nishu wrote, On 01/03/07 07:50:
    > Hi All,
    >
    > My Aim here is to increment lArray whether control goes inside if
    > statement or not. Is this a portable step and C guarantees that lArray
    > must be incremented?
    >
    > #include<stdio.h>
    >
    > int main(void)
    > {
    > int i = 7;
    > int array[] = {0,1,1,0,1,1,0};


    Apart from the fact I would not write code like this, I would rearrange
    the above two declarations to
    int array[] = {0,1,1,0,1,1,0};
    int i = (sizeof array) / sizeof *array;
    Then you don't have to change the initialisation of i if you change the
    size of the array.

    > int* lArray;
    >
    > lArray = array;
    >
    > while(i--)
    > {
    > if(*lArray++) /* Is this a portable step? */


    Yes, it is completely portable, and almost equivalent to:
    lArray++;
    if (*(lArray-1))

    I say almost because of sequence points which would be more important
    with a more complex expression.

    > {
    > printf("enters in if loop\n");
    > }
    > }
    > return 0;
    >
    > }

    --
    Flash Gordon
     
    Flash Gordon, Mar 1, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    836
  2. Replies:
    104
    Views:
    11,163
    Jordan Abel
    Oct 28, 2005
  3. Replies:
    99
    Views:
    2,560
    eliza81
    Jun 11, 2010
  4. Alf P. Steinbach /Usenet
    Replies:
    0
    Views:
    937
    Alf P. Steinbach /Usenet
    May 22, 2011
  5. Peng Yu

    post increment or pre increment?

    Peng Yu, Nov 21, 2008, in forum: Perl Misc
    Replies:
    7
    Views:
    593
    Peter J. Holzer
    Nov 23, 2008
Loading...

Share This Page