c - Run-Time Check Failure #2 - Stack around the variable 'input' was corrupted -



c - Run-Time Check Failure #2 - Stack around the variable 'input' was corrupted -

i trying write short simple c programme trying store 5 numbers array , print them on screen.

code following:

#define _crt_secure_no_warnings #define size 5 #include <stdio.h> #include <stdlib.h> int main (void){ int i,input[size]; printf("please come in %d numbers",size); (i=0; i<=size-1;++i);{ scanf("%d",&input[i]); printf ("numbers entered are: \n"); printf("%d",input[i]);} homecoming 0; }

when inputting 1 2 3 4 5 5 numbers store them in array input[size], getting next error :

run-time check failure #2 - stack around variable 'input' corrupted.

i know error array input[size] overflow. can't figure out how 1 2 3 4 5 overflowing.

thanks in advance.

you have wrong semicolon here:

for (i=0; i<=size-1;++i);{ // <---- wrong semicolon, braces scanf("%d",&input[i]); printf ("numbers entered are: \n"); printf("%d",input[i]);} // <---- ...closing brace here?

your loop accomplishes nothing. after completes, i==5, , first value written scanf @ input[5] (overrunning buffer).

additionally, brackets create no sense , not in right locations. if want first read 5 numbers, print message, print 5 numbers, need iterate twice.

so clear, here code, standard indentation / bracket placement. should clear doesn't create sense:

int main (void){ int i, input[size]; printf("please come in %d numbers",size); (i=0; i<=size-1; ++i) { // nil } // i=5 // pointless block { scanf("%d",&input[i]); // i=5, overrunning buffer printf ("numbers entered are: \n"); printf("%d",input[i]); } homecoming 0; }

here's code should (with proper indentation , brackets):

#define _crt_secure_no_warnings #define size 5 #include <stdio.h> #include <stdlib.h> int main (void) { int i; int input[size]; printf("please come in %d numbers:\n",size); (i=0; i<=size-1; ++i) { scanf("%d", &input[i]); } printf("\nnumbers entered are: \n"); (i=0; i<=size-1; ++i) { printf("%d\n", input[i]); } homecoming 0; }

input/output:

class="lang-none prettyprint-override">please come in 5 numbers: 6 5 4 3 2 numbers entered are: 6 5 4 3 2

c

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -