Array Arithmetic in C: Incrementing Integers in an Array

  • Thread starter Thread starter pivoxa15
  • Start date Start date
  • Tags Tags
    Arithmetic Array
Click For Summary

Discussion Overview

The discussion centers around the topic of incrementing integers in an array using C programming. Participants explore the validity of using the expression f[i]+=1 within a for loop, addressing concerns about array indexing and potential out-of-bounds errors.

Discussion Character

  • Technical explanation
  • Exploratory
  • Homework-related

Main Points Raised

  • One participant questions the validity of using f[i]+=1 in a for loop and seeks clarification on how to increment integers in an array.
  • Another participant states that they see nothing wrong with the expression and confirms it works correctly in their experience.
  • A different participant explains that f[i]+=1 is valid as long as i is within the bounds of the array (0 to 4), but warns that using an out-of-bounds index can lead to difficult-to-trace bugs.
  • One participant provides a code snippet demonstrating how to increment array elements within a for loop, encouraging testing as a learning method.

Areas of Agreement / Disagreement

Participants generally agree that the expression can be used correctly within the bounds of the array, but there is a caution regarding the risks of out-of-bounds access. The discussion does not reach a consensus on the best practices for handling such cases.

Contextual Notes

There are limitations regarding the assumptions about the value of i and the potential consequences of accessing memory outside the defined array bounds. The discussion does not resolve these concerns.

Who May Find This Useful

This discussion may be useful for individuals learning C programming, particularly those interested in array manipulation and debugging practices.

pivoxa15
Messages
2,250
Reaction score
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
I don't see anything wrong with it, but I tried it and it works just fine.
 
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.
 
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

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
3K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 13 ·
Replies
13
Views
5K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
1
Views
2K