Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > increment in if statement

Reply
Thread Tools

increment in if statement

 
 
Nishu
Guest
Posts: n/a
 
      03-01-2007
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;

}

 
Reply With Quote
 
 
 
 
Danny
Guest
Posts: n/a
 
      03-01-2007
On Mar 1, 9:50 am, "Nishu" <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      03-01-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Post increment ++ has higher precedence than pre increment ++. Why? Alf P. Steinbach /Usenet C++ 0 05-22-2011 12:03 PM
why prefix increment is faster than postfix increment? jrefactors@hotmail.com C++ 99 06-11-2010 12:51 PM
post increment or pre increment? Peng Yu Perl Misc 7 11-23-2008 11:44 PM
How do I do a conditional statement in a constant statement? tkvhdl@gmail.com VHDL 3 12-16-2005 06:13 PM
why prefix increment is faster than postfix increment? jrefactors@hotmail.com C Programming 104 10-27-2005 11:44 PM



Advertisments