Thread: Input to array loops forever [C]

  1. #1
    Registered User Quixiotic's Avatar
    Join Date
    Nov 2011
    Posts
    2

    Input to array loops forever [C]

    Alright I'm tackling an assignment and I just started with the input of numbers into an array. It seems to be working well, but what happens is once the for loop should be over, it repeats but not at zero like it should (If I wanted it to, which I don't). I'm confused so here's my code:
    Code:
    #include <stdio.h>
    
    
    #define ARRSIZE 19
    
    
    int main(){
    
    
     int temp[ARRSIZE];
     int press[ARRSIZE];
     int i;
    
    
     printf("\n--------------------------------------------------\n");
     printf("  Please enter a temperature and a pressure value.\n");
     for(i = 0; i <= ARRSIZE; i++){
       printf("%d: ", i+1);
       scanf("%d %d", &temp[i], &press[i]);
       } /* for() filling arrays with input */
    
    
     printf("Thank you!");
    return 0;
    } /* main() */
    When it starts the next iteration it starts with i=2 which I'm confused about only slightly less than why it's even looping again in the first place.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The code below results in undefined/unallocated space being used.

    Code:
    for(i = 0; i <= ARRSIZE; i++){
    change to the use of "<=" should be "<".

    Code:
    for(i = 0; i < ARRSIZE; i++){
    The location temp[ARRSIZE] is NOT in temp!

    Tim S.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > for(i = 0; i <= ARRSIZE; i++)
    I don't know about the start, but on the last iteration of this loop, you're stepping off the ends of the arrays (and possibly trampling all over your subscript).

    At which point, all bets are off.

    Use <ARRSIZE, not <=ARRSIZE
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User Quixiotic's Avatar
    Join Date
    Nov 2011
    Posts
    2
    Well thank you all so much, sure did fix the issue. So just that I have a better understanding of the issue, is the final iteration when it reaches 19 that it's going out of the range?

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Arrays start at zero an stop at SIZE - 1. So in your case 0 - 18.

    Jim

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Quixiotic View Post
    Well thank you all so much, sure did fix the issue. So just that I have a better understanding of the issue, is the final iteration when it reaches 19 that it's going out of the range?
    A common error for people to make is confusing "the number of elements" with "the number of the element".

    This is because we humans mostly start counting at 1... C starts at 0.

    As Jim explains if you have a 10 element array you have valid subscripts 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9 ... it's ok, count them, there are 10.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops forever
    By 7heavens in forum C Programming
    Replies: 8
    Last Post: 10-07-2009, 07:09 AM
  2. Using loops for check a roman number input.
    By eryell in forum C++ Programming
    Replies: 9
    Last Post: 04-12-2006, 11:04 AM
  3. Help / Loops; input redirection from files
    By Frank_Rye in forum C Programming
    Replies: 10
    Last Post: 10-16-2005, 01:15 AM
  4. Writing forever loops - using for(;;) or while(1)?
    By hzmonte in forum C Programming
    Replies: 8
    Last Post: 07-15-2005, 09:02 PM
  5. Live Forever??
    By tig in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 04-28-2002, 11:59 PM