increment in if statement

N

Nishu

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;

}
 
D

Danny

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.
 
F

Flash Gordon

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top