2个愚蠢的阵列问题!! [英] 2 stupid array questions!!

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

问题描述

嗨大师',

这是我的问题...


1)为什么c允许额外的,数组intialiser?这有什么优势吗?


ex:int arr [5] = {1,2,3,4,5,编译器没有给出错误!

>
函数参数?

ex:

void foo(int arr [])

{

/ *我想确定数组的大小arr在这个函数中.I

尝试*使用sizeof操作员并意识到它不会起作用

asarr将被视为指向int的指针!!!!!! * /

}


提前致谢

Hi Guru''s,
Here are my questions...

1)Why does c allows an extra "," in array intialiser?Is there any
advantage of this?

ex: int arr[5]={1,2,3,4,5,};
^^Compiler does not give error for this!

2)How to determine the size of the array which is passes as a
parameter to the function?
ex :
void foo(int arr[])
{
/*I want determine the size of the array "arr" in this function.I
tried *using "sizeof" operator and realised it is not going to work
as "arr" will be *treated as pointer to an int!!!!!!*/
}

Thanks in advance

推荐答案

" prashna" < VA ****** @ rediffmail.com>在留言中写道

news:d4 ************************** @ posting.google.c om ...

| 1)为什么c允许额外的,,在数组intialiser?有没有

|这个优势?

|

|例如:int arr [5] = {1,2,3,4,5,};

| ^^编译器没有给出错误!

它使代码生成器更容易。

除此之外没有真正的好处...


| 2)如何确定作为

|传递的数组的大小函数的参数?

|例如:

| void foo(int arr [])

| {

| / *我想确定数组的大小arr在这个函数中。我

|试过*使用sizeof操作员并意识到它不会起作用

|作为arr将被视为指向int的指针!!!!!! * /

| } $ / $
数组的大小需要作为第二个参数传递。

C中没有其他方式(C ++允许你需要的东西,通过模板...)。


hth,

Ivan

-
http://ivan.vecerina.com


"prashna" <va******@rediffmail.com> wrote in message
news:d4**************************@posting.google.c om...
| 1)Why does c allows an extra "," in array intialiser?Is there any
| advantage of this?
|
| ex: int arr[5]={1,2,3,4,5,};
| ^^Compiler does not give error for this!
It makes things easier for code generators.
No real benefit besides that...

| 2)How to determine the size of the array which is passes as a
| parameter to the function?
| ex :
| void foo(int arr[])
| {
| /*I want determine the size of the array "arr" in this function.I
| tried *using "sizeof" operator and realised it is not going to work
| as "arr" will be *treated as pointer to an int!!!!!!*/
| }
The size of the array needs to be passed as a second parameter.
No other way in C (C++ allows what you need, through templates...).

hth,
Ivan
--
http://ivan.vecerina.com



prashna写道:
prashna wrote:

嗨大师的,
以下是我的问题...

1)为什么c允许额外的, "数组intialiser?这有什么优势吗?

ex:int arr [5] = {1,2,3,4,5,};
^^编译器没有给出错误!

2)如何确定作为函数的
参数传递的数组大小?
ex:
void foo(int arr [])
{
/ *我想确定数组的大小arr在这个函数中。我尝试*使用sizeof操作员,并意识到它不会工作
作为arr将被视为指向int的指针!!!!!! * /
}

Hi Guru''s,
Here are my questions...

1)Why does c allows an extra "," in array intialiser?Is there any
advantage of this?

ex: int arr[5]={1,2,3,4,5,};
^^Compiler does not give error for this!

2)How to determine the size of the array which is passes as a
parameter to the function?
ex :
void foo(int arr[])
{
/*I want determine the size of the array "arr" in this function.I
tried *using "sizeof" operator and realised it is not going to work
as "arr" will be *treated as pointer to an int!!!!!!*/
}




如果你想让foo知道大小,

然后你必须给foo更多信息。


void foo(int arr [],size_t n_elem)/ *元素数量* /


void foo(int arr [],size_t bytes)/ * size in bytes * /


-

pete



If you want foo to know the size,
then you have to give foo more information.

void foo(int arr[], size_t n_elem) /* number of elements */

void foo(int arr[], size_t bytes) /* size in bytes */

--
pete


在文章< d4 ************************** @ posting.google.com> ;,prashna写道:
In article <d4**************************@posting.google.com >, prashna wrote:
嗨大师',
这是我的问题......

1)为什么c允许额外的, ;数组intialiser?这有什么优势吗?

ex:int arr [5] = {1,2,3,4,5,};
^^编译器没有给出错误!


它简化了生成C代码的代码。


哦,你的例子中不需要[5]中的5。

2)如何确定作为函数的
参数传递的数组的大小?
Hi Guru''s,
Here are my questions...

1)Why does c allows an extra "," in array intialiser?Is there any
advantage of this?

ex: int arr[5]={1,2,3,4,5,};
^^Compiler does not give error for this!
It simplifies code that generate C code.

Oh, and the 5 in [5] is not needed in your example.

2)How to determine the size of the array which is passes as a
parameter to the function?



[cut]

更改函数的接口,以便调用者也可以发送数组的长度(即添加一个额外的int参数)。

-

Andreas K?h?ri


[cut]
Change the interface of the function so that the caller also
sends the length of the array (i.e. add an extra int argument).
--
Andreas K?h?ri


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

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