scanf函数和2-d数组 [英] scanf function and 2-d arrays

查看:86
本文介绍了scanf函数和2-d数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有二维数组和scanf函数的问题。

我已经知道了一个代码,其中我接受一维数组中的值

int a [5],i;

for(i = 0; i< 3; i ++)

scanf("%d" ;,a + i);

当我打印值时,它显示了正确的值,但是当我为b-b
编写了相同的二维数组代码时,

int a [2] [2],i,j;

for(i = 0; i< 2; i ++)

for( j = 0; j< 2; j ++)

scanf("%d",a + i + j);
打印后的
,它显示了我的垃圾值a [0] [1]和[1] [1]是

因为我的编译器没有处理这种

表达式的程序。

为什么会这样,有人可以告诉我,

欢呼

madhura

Hello,
I have a problem with 2-d arrays and scanf function.
I have wriitten a code in which i am accepting values in 1-d arrays
int a[5],i;
for(i=0;i<3;i++)
scanf("%d",a+i);
when I print the values, it showed me the correct values,but when i
wrote the same code for 2-d arrays,
int a[2][2],i,j;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",a+i+j);
after printing,it showed me garbage values for a[0][1] and a[1][1], is
it because my compiler doesnt have the program for handling such
expressions.
why this happened, can anybody tell me,
cheers
madhura

推荐答案

madhura写道:
madhura wrote:
你好,
我有二维数组和scanf函数的问题。
我已经接受了一个我接受的代码1-d数组中的值
int a [5],i;
for(i = 0; i <3; i ++)
scanf("%d",a + i) ;当我打印这些值时,它显示了正确的值,但是当我为二维数组编写相同的代码时,
int a [2] [2],i, j;
for(i = 0; i< 2; i ++)
for(j = 0; j< 2; j ++)
scanf("%d",a + i + j);
打印后,它显示了[0] [1]和[1] [1]的垃圾值,是因为我的编译器没有处理这样的程序/>表达。
为什么会这样,有人可以告诉我,
欢呼
madhura
Hello,
I have a problem with 2-d arrays and scanf function.
I have wriitten a code in which i am accepting values in 1-d arrays
int a[5],i;
for(i=0;i<3;i++)
scanf("%d",a+i);
when I print the values, it showed me the correct values,but when i
wrote the same code for 2-d arrays,
int a[2][2],i,j;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",a+i+j);
after printing,it showed me garbage values for a[0][1] and a[1][1], is
it because my compiler doesnt have the program for handling such
expressions.
why this happened, can anybody tell me,
cheers
madhura




for(i = 0; i< ; 2; i ++)

for(j = 0; j< 2; j ++)

scanf("%d",& a [i] [j] );


for(i = 0; i< 2; i ++)

for(j = 0; j< 2; j ++)

printf("%d \ n",a [i] [j]);


考虑一下此输出的差异...


for(i = 0; i< 2; i ++)

for(j = 0; j< 2; j ++)

printf (QUOT;%p\t%p\\\
&曲ot;,(void *)(a + i + j),(void *)(& a [i] [j]));


更清楚?

-

==================================== ============== =============

试图减少''上的不必要的噪音''信号''...


免责声明:


我提供的任何评论/代码可能= = = 100%便携,也不是

语义正确[读 - ''不是100%迂腐正确'']。

我不太关心这个,我认为这是

与大多数访客相同。但是,请放心,任何

''必要''(?)更正几乎肯定会出现v.soon

[读 - ''加上他们认为合适的噪音,*一个学究*将会很快就会在

''。


警告:请务必阅读标签。没有旁边的细节

过滤器提供。远离儿童。不要点燃。

====================================== ============ =============



for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);

for(i=0;i<2;i++)
for(j=0;j<2;j++)
printf("%d\n",a[i][j]);

Consider the difference in the output of this ...

for(i=0;i<2;i++)
for(j=0;j<2;j++)
printf("%p\t%p\n",(void *)(a+i+j),(void *)(&a[i][j]));

Clearer?
--
================================================== =============
In an attempt to reduce ''unwanted noise'' on the ''signal'' ...

Disclaimer:

Any comment/code I contribute might =NOT= be 100% portable, nor
semantically correct [read - ''not 100% pedantically correct''].
I don''t care too much about that though, and I reckon it''s the
same with most ''visitors'' here. However, rest assured that any
''essential'' (?) corrections WILL almost certainly appear v.soon
[read - ''to add noise as they see fit, *a pedant* will be along
shortly''].

WARNINGS: Always read the label. No beside-the-point minutiae
filter supplied. Keep away from children. Do not ignite.
================================================== =============


madhura写道:
madhura wrote:
你好,
我有二维数组和scanf函数的问题。
我有一个代码,其中我接受1-d
数组中的值int a [5],i;
for(i = 0; i< 3; i ++)
scanf("%d",a + i);
当我打印数值时,它向我展示了正确的值,但是当我为二维数组编写相同的代码时,
int a [2] [2],i,j;
for(i = 0; i< 2; i ++)
for(j = 0; j< 2; j ++)
scanf("%d",a + i + j);


想一想最后一行,它可能会告诉你

你的问题在哪里。是[i] [j]和*(a + i + j)一样吗?


干杯


弗拉基米尔
打印后
,它显示了[0] [1]和
a [1] [1]的垃圾值,是不是因为我的编译器没有用于处理这些表达式的程序。
为什么会这样,有人可以告诉我,
欢呼
madhura
Hello,
I have a problem with 2-d arrays and scanf function.
I have wriitten a code in which i am accepting values in 1-d
arrays int a[5],i;
for(i=0;i<3;i++)
scanf("%d",a+i);
when I print the values, it showed me the correct values,but
when i wrote the same code for 2-d arrays,
int a[2][2],i,j;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",a+i+j);
Have a think about this last line, and it may tell you where
your problem is. Is a[i][j] the same as *(a+i+j)?

Cheers

Vladimir
after printing,it showed me garbage values for a[0][1] and
a[1][1], is it because my compiler doesnt have the program for
handling such expressions.
why this happened, can anybody tell me,
cheers
madhura






madhura写道:
madhura wrote:
你好,
我有二维数组和scanf函数的问题。
我有一个代码,其中我接受一维数组中的值
int a [5],i;
for(i = 0; i< 3; i ++)
scanf("%d",a + i);
当我打印时值,它显示了正确的值,但是当我为二维数组编写相同的代码时,
int a [2] [2],i,j;
for(i = 0; i< 2; i ++)
for(j = 0; j <2; j ++)
scanf(%d,a + i + j);
,它显示了[0] [1]和[1] [1]的垃圾值,是因为我的编译器没有hav处理此类表达的程序。
为什么会发生这种情况,任何人都可以告诉我,
Hello,
I have a problem with 2-d arrays and scanf function.
I have wriitten a code in which i am accepting values in 1-d arrays
int a[5],i;
for(i=0;i<3;i++)
scanf("%d",a+i);
when I print the values, it showed me the correct values,but when i
wrote the same code for 2-d arrays,
int a[2][2],i,j;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",a+i+j);
after printing,it showed me garbage values for a[0][1] and a[1][1], is
it because my compiler doesnt have the program for handling such
expressions.
why this happened, can anybody tell me,




如果你坚持使用''+' '运算符为了对数组

元素执行索引访问,那么二维数组的相应表达式看起来如下

如下


*(a + i)+ j


这相当于


& a [i] [j]


这就是你需要的东西(为什么你不坚持那个

后一种形式?)。


至于为什么?部分,它已在此处和其他地方不止一次解释。

您可以从常见问题解答中查找相应部分开始: http://c-faq.com


-

祝你好运,

Andrey



If you insist on using ''+'' operator in order to perform index access to array
elements, then the corresponding expression for 2-dimensional array would look
as follows

*(a + i) + j

That would be equivalent to

&a[i][j]

which is what you need in your case (and why don''t you just stick with that
latter form?).

As for the "why?" part, it has been explained here and elsewhere more than once.
You can start by looking up the corresponding section in the FAQ: http://c-faq.com.

--
Best regards,
Andrey


这篇关于scanf函数和2-d数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆