## array arithmetic in C?

int f[5];

Is it possible to do

f[i]+=1;

inside a valid for loop?

If not than how can I increment integers stored in an arrary for each i in C?

Thanks
 PhysOrg.com science news on PhysOrg.com >> New language discovery reveals linguistic insights>> US official: Solar plane to help ground energy use (Update)>> Four microphones, computer algorithm enough to produce 3-D model of simple, convex room
 Recognitions: Homework Help I don't see anything wrong with it, but I tried it and it works just fine.

Mentor
 Quote by pivoxa15 int f[5]; Is it possible to do f[i]+=1;
Sure, so long as i has a valid value for an index to that array, namely in the range 0...4.

Actually, a C compiler will cheerfully let you use a value of i outside that range, but then you'll be incrementing the contents of some memory location outside the array. This sort of thing produces bugs that can be very difficult to track down unless you step through the code with a debugger.

## array arithmetic in C?

One of the best ways to learn C is to test these sorts of questions.

Code:
#include <stdio.h>

main()
{
int f[5]={0}, i;

for(i=0;i<5;i++)
{
printf("               i = %d \n", i);
printf("            f[i] = %d \n", f[i]);
f[i]+=i;
printf("incremented f[1] = %d \n", f[i]);
}

}
Don't be afraid to test these things when you have a question. A simple program like the one above will usually clear things up in a jiffy.

 Similar Threads for: array arithmetic in C? Thread Forum Replies Programming & Comp Sci 3 Programming & Comp Sci 1 Programming & Comp Sci 3 General Astronomy 7 General Math 3