program bug

i'm writing a program to obtain elements of an array as input from a user. i was able to figure out how to write everything, but when i run it the program crashes after obtaining all the elements from the user. what did i do wrong? also, is there a way to simpler obtain input with spaces from the user and store them into the array as well as determining how many were entered? i.e. a gets() function for an int array and some way to determine the number of values entered.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
#define arraysize 100

void revArray( int size, int arry[] )
{
     int i;
     for ( i = size-1; i >= 0; i++)
         printf("%d", arry[i]);
}

int main()
{
    int inArray[arraysize] = {};
    int size = 0;
    int i;
    printf("enter array size: ");
    scanf(" %d", &size);
    for( i = 0; i < size; i++)
    {
       printf( "enter element %d of the string: ", i + 1);
       scanf(" %d", &inArray[i]);
    }
    printf( "the array values in reverse order are: ");
    revArray( size, inArray);
    system("pause");
    return 0;
}
Last edited on
I'm not sure if it holds for C, but in C++ when using for statements it's standard practice to declare the counter variable (i in both cases) within the first part of the for, like this:

for (int i = 0; i < size; i++)

This way the variable is local to the for statement block alone and won't be seen outside of it. The way you are doing it the i declared in main has scope from the declaration to the end of the program. This could cause other conflicts in larger programs.

But, after saying all that, I'm not sure that's the problem here. I'm not used to C but I'm guessing the crash is being caused by something else.

Edit: Now that I am looking at it again, I think the crash is in your function to reverse the array. In your for statement you use ;i >=0; i++ This should print out the very last element of the array (since you are setting the i within this function to = arraysize - 1 or 99), then increment it. The very next iteration will be trying to print arry[100] which is out of bounds for the array. What I imagine you wanted to do was read the last element of the array and go backwards. For that change line 7 to

for (i = size - 1; i >= 0; i--)

This will decrement the counter from 99 to 0, basically printing out the last number entered and working to the first.
Last edited on
thanks, the decrement thing was the problem. at least it was only reading values and not a buffer overflow. thanks again
also, the variable in for statements in C must be declared outside the statement, then initialized within the control statement. I started on c++ too, so i know it looks odd but C doesn't seem to support the c++ method.
C supports that just fine, unless you're taking about twice obsolete C89
if i try doing it how i do in c++ it gives me a something C99 error. first time i got it i searched and the answers i found said to declare it outside the statement. If it supports it, does my compiler need to be updated or something?
closed account (Dy7SLyTq)
try -std:c++11
Topic archived. No new replies allowed.