Array Arithmetic in C: Incrementing Integers in an Array

In summary, it is possible to increment integers stored in an array using the f[i]+=1 notation within a valid for loop. However, it is important to ensure that the value of i is within the range of the array to avoid unintentionally incrementing the contents of other memory locations. It is recommended to test these situations with a simple program to better understand how they work.
  • #1
pivoxa15
2,255
1
int f[5];

Is it possible to do

f+=1;

inside a valid for loop?

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

Thanks
 
Technology news on Phys.org
  • #2
I don't see anything wrong with it, but I tried it and it works just fine.
 
  • #3
pivoxa15 said:
int f[5];
Is it possible to do
f+=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.
 
  • #4
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.
 

1. What is array arithmetic in C?

Array arithmetic in C refers to the process of performing mathematical operations on elements within an array. This can include incrementing or decrementing integer values in an array.

2. How do you increment integers in an array in C?

To increment integers in an array in C, you can use a for loop to iterate through the array and use the ++ operator to increase the value of each element by 1.

3. Can you provide an example of incrementing integers in an array in C?

Yes, for example:

int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
    arr[i]++;
}
//The array now contains {2, 3, 4, 5, 6}

4. What other arithmetic operations can be performed on arrays in C?

In addition to incrementing or decrementing integers, other arithmetic operations that can be performed on arrays in C include addition, subtraction, multiplication, and division. These operations can be applied to individual elements in the array or to the entire array as a whole.

5. Are there any limitations to array arithmetic in C?

Yes, there are some limitations to array arithmetic in C. Arrays in C have a fixed size and cannot be resized during runtime, so the number of elements in the array cannot be changed. Additionally, array arithmetic may not work as intended if the array contains elements of different data types.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
32
Views
2K
  • Programming and Computer Science
Replies
5
Views
988
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
13
Views
3K
  • Programming and Computer Science
Replies
1
Views
942
  • Programming and Computer Science
Replies
4
Views
3K
Back
Top