The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Array input problem
Discuss Array input problem in the C Programming forum on Dev Shed. Array input problem C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 13th, 2012, 10:41 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 10
  
Time spent in forums: 8 h 16 m 23 sec
Reputation Power: 0
|
|
|
Array input problem
Hello everybody i am pretty newbie in c and general on programming so take it easy with me..
So my question is
i have here this code
PHP Code:
#include <stdio.h>
#include <stdlib.h>
main(){
int ar[2][2];
int i,j;
for (i=0;i<=2;i=i+1)
for (j=0;j<=2;j=j+1){
printf(" i = %d j = %d ",i,j);
scanf("%d",&ar[i][j]);
}
for (i=0;i<=2;i=i+1){
printf("\n");
for (j=0;j<=2;j=j+1)
printf("%d for i= %d, j= %d ",ar[i][j],i,j);
}
printf("\nwrong values are .. %d %d %d",ar[0][2],ar[1][2],ar[2][2]);
scanf("%d",&i);
}
i put in the values 1,2,3 ... to 9 respectivly but it outputs me this h t t p: / / imageshack.us/photo/my-images/840/006503.jpg/
i didnt know any other way to refer people to this image so i had to put this link
Thanks.
|

October 13th, 2012, 11:30 AM
|
|
|
The array (of arrays) defined with
has 4 elements. Those elements are: ar[0][0], ar[0][1], ar[1][0], and ar[1][1]. You are trying to access elements that do not exist, namely those with 2 as one of the indexes (eg ar[0][2], or ar[2][2]).
Try defining your array with
|

October 13th, 2012, 11:31 AM
|
 |
Contributed User
|
|
|
|
|
> for (i=0;i<=2;i=i+1)
The valid subscripts of ar[2][2] are [0][0], [0][1], [1][0] and [1][1]
You're running off the ends of the array.
Use <, not <= in all your loops.
|

October 13th, 2012, 11:50 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 10
  
Time spent in forums: 8 h 16 m 23 sec
Reputation Power: 0
|
|
Really thank you for the help.
BUT
Code:
#include <stdio.h>
#include <stdlib.h>
main(){
int ar[3][3];
int i,j;
for (i=1;i<=3;i=i+1)
for (j=1;j<=3;j=j+1){
printf(" i = %d j = %d ",i,j);
scanf("%d",&ar[i][j]);
}
for (i=1;i<=3;i=i+1){
printf("\n");
for (j=1;j<=3;j=j+1)
printf("%d for i= %d, j= %d ",ar[i][j],i,j);
}
printf("\nwrong values are .. %d %d %d",ar[1][3],ar[2][3],ar[3][3]);
scanf("%d",&i);
}
can you explain me why this one works?(ar[1,2,3,][3] dont exist as you said)
And on the previous code how the computer got access to the ar[0][2] ar[1][2]..
and put those specific (wrong) values, was it random values?
Again thanks for the help.
|

October 13th, 2012, 12:11 PM
|
|
|
|
> Can you explain why this one works?
Accessing elements that do not exist, in C, is Undefined Behavior, meaning the compiler can do anything: it can complain and stop compilation, it can complain but compile anyway. Furthermore the resulting executable (if any) can do anything: it can crash, it can work with apparent errors, or it can work as you would expect (the most difficult UB to catch).
To answer your question: that one works because you were unlucky.
I see you changed the loop to start accessing the array elements at index 1 (it was index 0 in your previous post). In C, array indexes start at 0. Your first post was correct in this regard.
|

October 13th, 2012, 12:47 PM
|
 |
Contributing User
|
|
|
|
|
How many non-negative integers are there less than 3 ?
0 1 2
That's right, 3 of them.
Less than n where n is a non-negative integer?
That's right, n of them.
__________________
[code] Code tags[/code] are essential for python code!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|