| Thread Closed |
array arithmetic in C? |
Share Thread |
| Oct19-05, 07:56 AM | #1 |
|
|
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 |
| Oct19-05, 01:43 PM | #2 |
|
Recognitions:
|
I don't see anything wrong with it, but I tried it and it works just fine.
|
| Oct19-05, 02:20 PM | #3 |
|
Mentor
|
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. |
| Oct19-05, 02:31 PM | #4 |
|
|
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]);
}
}
|
| Thread Closed |
Similar Threads for: array arithmetic in C?
|
||||
| Thread | Forum | Replies | ||
| C++ complex array | Programming & Comp Sci | 3 | ||
| How can I tell when I've reached the end of an array? | Programming & Comp Sci | 1 | ||
| converting from an array of function values to coordinate array of different length | Programming & Comp Sci | 3 | ||
| An array of questions. | General Astronomy | 7 | ||
| Average of an array | General Math | 3 | ||