我应该声明为函数的参数传递一个数组的预期大小? [英] Should I declare the expected size of an array passed as function argument?

查看:71
本文介绍了我应该声明为函数的参数传递一个数组的预期大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为无论是有效的C语法,但哪个好?

A)

 无效FUNC(int类型的[]); //函数原型
无效FUNC(int类型的[]){/ * ... * /} //函数定义

B)

 的#define ARRAY_SIZE 5
无效FUNC(INT A [ARRAY_SIZE]); //函数原型
无效FUNC(INT A [ARRAY_SIZE]){/ * ... * /} //函数定义


解决方案

我看第二种形式没有用。编译器也运行既不可以使用这个指示。你仍然能够通过错误的访问超越极限。所以它只是几个额外的字符。最好是通过数组作为​​第二个参数的实际长度。那么你可以真正使用它的功能。

I think both is valid C syntax, but which is better?

A)

void func(int a[]);  // Function prototype
void func(int a[]) { /* ... */ }  // Function definition

or

B)

#define ARRAY_SIZE 5
void func(int a[ARRAY_SIZE]);  // Function prototype
void func(int a[ARRAY_SIZE]) { /* ... */ }  // Function definition

解决方案

i see no use in second form. neither compiler nor runtime can use this indication. still you are able to access beyond limits by mistake. so it is just few more extra characters. it is better to pass actual length of array as second parameter. then you can really use it in function.

这篇关于我应该声明为函数的参数传递一个数组的预期大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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