排列 [英] array

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

问题描述

为什么数组不能被分配,比如结构?

解决方案

3月7日,10:26 pm,buuuu ... @ gmail.com写道:


为什么数组不能分配,比如结构?



因为数组可以传递给函数或其他东西,并且

编译器不能总是知道数组有多大。 (这是我能想到的唯一的原因,几乎可以肯定其他人。)

另一方面,结构总是包含相同的内容,所以<编译器可以分配它们的



当然,如果你想将一个数组分配给另一个,你可以使用

memcpy()函数(如果参数可能重叠,则为memmove()):


int array [] = {1,2,3,4,5};

int toarray [sizeof(array)/ sizeof(* array)];


memcpy(toarray,array,sizeof(array));


" dwks" < ki ********** @ primus.cawrites:


3月7日,晚上10:26,buuuu ... @ gmail。 com写道:


>为什么数组不能被分配,比如结构?



因为数组可以传递给函数或其他东西,而且

编译器不能总是知道数组有多大。 (这是我能想到的唯一的原因,几乎可以肯定其他人。)

另一方面,结构总是包含相同的内容,所以

编译器可以分配它们。



[...]


不,数组不能传递给函数。你可以做*看起来*

就像将一个数组传递给一个函数一样,但你真的只是将一个

指针传递给它的第一个元素。


数组在C中不是一流的对象。一流并不是一个定义明确的概念,但基本上它意味着你可以用其他类型的东西来实现你不能用数组做的事情。没有必要的一些根本原因,这就是为什么(这不是其他语言中的b
);这就是语言恰好是设计的方式,受其前辈B和BCPL的影响。


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst> ;

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长


bu ****** @ gmail.com 写道,On 08/03/07 05:26:


为什么不能像结构一样分配数组?



因为语言不允许。

-

Flash Gordon


why array can''t be assigned, like structs?

解决方案

On Mar 7, 10:26 pm, buuuu...@gmail.com wrote:

why array can''t be assigned, like structs?

Because an array can be passed to a function, or something, and the
compiler can not always know how big the array is. (This is the only
reason I can think of offhand, there are almost certainly others.)
Structures, on the other hand, always contain the same contents and so
the compiler can assign them.

Of course, if you want to assign one array to another, you can use the
memcpy() function (or memmove() if the parameters might overlap):

int array[] = {1, 2, 3, 4, 5};
int toarray[sizeof(array)/sizeof(*array)];

memcpy(toarray, array, sizeof(array));


"dwks" <ki**********@primus.cawrites:

On Mar 7, 10:26 pm, buuuu...@gmail.com wrote:

>why array can''t be assigned, like structs?


Because an array can be passed to a function, or something, and the
compiler can not always know how big the array is. (This is the only
reason I can think of offhand, there are almost certainly others.)
Structures, on the other hand, always contain the same contents and so
the compiler can assign them.

[...]

No, an array can''t be passed to a function. You can do what *looks*
like passing an array to a function, but you''re really just passing a
pointer to its first element.

Arrays aren''t first-class objects in C. "First-class" isn''t a
well-defined concept, but basically it means that there are things you
can do with other types that you can''t do with arrays. There isn''t
necessarily some fundamental reason why this is so (it isn''t in some
other languages); it''s just the way the language happens to be
designed, influenced by its predecessors B and BCPL.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


bu******@gmail.com wrote, On 08/03/07 05:26:

why array can''t be assigned, like structs?

Because the language does not allow it.
--
Flash Gordon


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

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