数组初始化 [英] Array Initialization

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

问题描述

当我得到这个时,我终于认为我对C / $
中的多维数组有所了解:


#include< stdio.h>


#define max_x 3

#define max_y 5

int array [max_x] [max_y];


main()

{

int x,y;


for(x = 0; x< max_x; x ++)

for(y = 0; y< max_y; y ++)

array [x] [y] = x * 10 + y;


for(y = 0; y< max_y; y ++){

(void)printf(" array [%d]" ;,y);

for(x = 0; x< max_x; x ++)

(void)printf("%d",array [x, y]);

(无效)printf(" \ n");

}


返回0;

}


这个程序初始化为:

array [0] 56164 56164 56164

array [ 1] 56184 56184 56184

阵列[2] 56204 56204 56204

阵列[3] 56224 56224 56224

阵列[4] 56244 56244 56244


Cou有人请帮我弄清楚这个程序如何到达这样一个

大号?

I finally thought I had an understanding of multi dimensional arrays in C
when I get this:

#include <stdio.h>

#define max_x 3
#define max_y 5

int array[max_x][max_y];

main()
{
int x,y;

for(x = 0; x < max_x; x++)
for(y=0; y < max_y; y++)
array[x][y] = x * 10 + y;

for(y = 0; y < max_y; y++) {
(void)printf("array[%d] ", y);
for(x = 0; x < max_x; x++)
(void)printf("%d ", array[x,y]);
(void)printf("\n");
}

return 0;
}

This program initializes to :
array[0] 56164 56164 56164
array[1] 56184 56184 56184
array[2] 56204 56204 56204
array[3] 56224 56224 56224
array[4] 56244 56244 56244

Could someone please help me figure out how this program arrives at such a
large number?

推荐答案

>主题:数组初始化
>Subject: Array Initialization
From:" Henry" he*****@knology.net
日期:9/2/03 3:27 PM夏威夷标准时间
消息ID:< vl ************ @ corp.supernews.com>

我终于认为我对多维数组有所了解在C
当我得到这个:

#include< stdio.h>

#define max_x 3
#define max_y 5
int数组[max_x] [max_y];

main()
对于(x,x,y;

(0 = 0; y 数组[x] [y] = x * 10 + y;

for(y = 0; y< max_y; y ++){
(void)printf(" array [%d]",y);
for(x = 0; x < max_x; x ++)
(void)printf("%d",array [x,y]);
(void)printf(" \ n");
}

返回0;
}
这个程序初始化为:
数组[0] 56164 56164 56164
数组[1] 56184 56184 56184
array [2] 56204 56204 56204
array [3] 56224 56224 56224
array [4] 56244 56244 56244

有人可以帮我弄清楚这个程序如何到达如此大的数字?
From: "Henry" he*****@knology.net
Date: 9/2/03 3:27 PM Hawaiian Standard Time
Message-id: <vl************@corp.supernews.com>

I finally thought I had an understanding of multi dimensional arrays in C
when I get this:

#include <stdio.h>

#define max_x 3
#define max_y 5

int array[max_x][max_y];

main()
{
int x,y;

for(x = 0; x < max_x; x++)
for(y=0; y < max_y; y++)
array[x][y] = x * 10 + y;

for(y = 0; y < max_y; y++) {
(void)printf("array[%d] ", y);
for(x = 0; x < max_x; x++)
(void)printf("%d ", array[x,y]);
(void)printf("\n");
}

return 0;
}

This program initializes to :
array[0] 56164 56164 56164
array[1] 56184 56184 56184
array[2] 56204 56204 56204
array[3] 56224 56224 56224
array[4] 56244 56244 56244

Could someone please help me figure out how this program arrives at such a
large number?




哎呀,我很想知道它是如何编译的。


(void)printf("%d",array [x,y]);


应该是.. array [x] [y] ....


Fortran很难死,不是吗? :-)


Stuart

Stuart A. Weinstein博士

Ewa海滩构造研究所

错误是人类,但要真正搞砸了

需要创造论者



Heck, I''m interested to know how this even compiled.

(void)printf("%d ", array[x,y]);

Should be .. array[x][y]....

Fortran dies hard, don''t it? :-)

Stuart
Dr. Stuart A. Weinstein
Ewa Beach Institute of Tectonics
"To err is human, but to really foul things up
requires a creationist"


2003年9月2日星期二21:27 :36-0400,亨利 < he ***** @ knology.net>

写道:
On Tue, 2 Sep 2003 21:27:36 -0400, "Henry" <he*****@knology.net>
wrote:
我终于认为我对C中的多维数组有了一个了解当我得到这个时:

#include< stdio.h>

#define max_x 3
#define max_y 5
int array [max_x] [max_y];

main()
{x / y;

for(x = 0; x < max_x; x ++)
for(y = 0; y< max_y; y ++)
array [x] [y] = x * 10 + y;

(y = 0; y (void)printf(" array [%d]",y);
for(x = 0; x< max_x; x ++)
(void)printf("%d",array [x,y]);


(无效)printf("%d",array [x] [y]);


array [x,y ]使用逗号运算符并且与数组[y]相同。


您正在调用UB,因为数组[y]具有类型''int *''和

你用%d打印它。

(无效)printf(" \ n");
}

返回0;
}
这个程序初始化为:
数组[0] 56164 56164 56164
数组[1] 56184 56184 56184
数组[2] 56204 56204 56204
阵列[3] 56224 56224 56224
阵列[4] 56244 56244 56244

有人可以帮我弄清楚这个程序如何到达这样一个
大数?
I finally thought I had an understanding of multi dimensional arrays in C
when I get this:

#include <stdio.h>

#define max_x 3
#define max_y 5

int array[max_x][max_y];

main()
{
int x,y;

for(x = 0; x < max_x; x++)
for(y=0; y < max_y; y++)
array[x][y] = x * 10 + y;

for(y = 0; y < max_y; y++) {
(void)printf("array[%d] ", y);
for(x = 0; x < max_x; x++)
(void)printf("%d ", array[x,y]);
(void)printf("%d ", array[x][y]);

array[x,y] uses the comma operator and is the same as array[y].

You''re invoking UB because array[y] has type ''int *'' and
you''re printing it with %d.
(void)printf("\n");
}

return 0;
}

This program initializes to :
array[0] 56164 56164 56164
array[1] 56184 56184 56184
array[2] 56204 56204 56204
array[3] 56224 56224 56224
array[4] 56244 56244 56244

Could someone please help me figure out how this program arrives at such a
large number?




尼克。



Nick.


文章< vl ******** ****@corp.supernews.com>,

Henry< he ***** @ knology.net>写道:
In article <vl************@corp.supernews.com>,
Henry <he*****@knology.net> wrote:
当我得到这个时,我终于认为我已经理解了C /中的多维数组:

int数组[max_x] [max_y];

(void)printf("%d",array [x,y]);
I finally thought I had an understanding of multi dimensional arrays in C
when I get this:

int array[max_x][max_y];

(void)printf("%d ", array[x,y]);




array [x,y] is与array [x] [y]不一样。

表达式:

x,y

是一个逗号表达式,其值为第二个

表达式的值(在本例中为y)。将评估第一个表达式(在本例中为x)

的副作用;但在这种情况下,有
无。所以x,y相当于:

y


所以你实际上有

printf("%d", array [y])

array [y]是type(指向int的指针)。 "%d"需要一个int。所以你是使用int的
格式说明符打印(指向int的指针);结果

未定义。将代码更改为:

printf("%d",array [x] [y]);

,您可能会得到您的预期。


顺便说一句,我很好奇你正在运行什么平台。 (即什么

编译器和什么操作系统和什么处理器)?你得到的数字让我感到有些惊讶。 (当然,这一切都是未定义的......但是在一个典型的PC平台上,我已经预料到实际结果要比b
大得多你得到的数字。)


- Brett



array[x,y] is not the same thing as array[x][y].
The expression:
x,y
is a comma expression whose value is the value of the second
expression (in this case, y). The first expression (in this case, x)
would be evaluated for its side effects; but in this case, there are
none. So x,y is equivalent to just:
y

So you effectively have
printf("%d ", array[y])

array[y] is type (pointer to int). "%d" requires an int. So you''re
using an "int" format specifier to print a (pointer to int); the result
is undefined. Change the code to:
printf("%d ", array[x][y]);
and you''ll probably get what you expected.

By the way, I''m curious what platform you are running on. (i.e. what
compiler and what OS and what processor)? I''m somewhat surprised at
the numbers you got. (It''s all undefined, of course ... but on a
typical PC platform, I''d have expected the actual result to be much
larger than the numbers you got.)

-- Brett


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

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