Close
Close

Use of pointers in array. (also about pre and post increment)

   anew

MY QUESTION IS ABOUT AN EXAMPLE FROM TOPIC ARRAY.

#include <stdio.h>
display(int *p)
{
    int i;
    for(i=0;i<8;++i)
    {
        printf("n[%d] = %d\n", i, *p);
        p++;
    }
}
int main() 
{
    int n[ ] = { 1, 2, 3, 4, 5, 6, 7, 8 }; 
    display(n); 
    return 0;
}

IN THIS PROGRAM ++i IS USED IN FOR LOOP.

I TRIED TO RUN THE PROGRAM BY USING i++ .

BOTH WAYS, I GOT THE SAME OUTPUT.

PLEASE EXPLAIN, WHY PRE INCREMENT AND POST INCREMENT DID NOT CAUSE ANY DIFFERENCE? 


Answers

  •   

    Normally, the i++ is inside a statement like: 

    int num = 3 + i++; 

    In this case, the argument is run and then 1 gets added to i. However, when it comes to a for loop, the i++ is run as its own argument after the body.


     



Ask Yours
Post Yours
Write your answer