数组作为函数的参数 [英] array as function argument

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

问题描述

考虑以下code:

#define MAX_COUNT 4
static int foo(int w[MAX_COUNT])
{
    int *p = w;

    if (w) {
        w[0] = 10; w[1] = 20; w[2] = 30; w[3] = 40;
    }

    return 0;
}

它便携式和法律传递NULL是定义foo的上述()(例如,对于一个情况时我不需要W []改变)?假设一个数组的名字是一个指向它的第一要素,在我看来,它应该是好的,但perhpas我失去了一些东西?

is it portable and legal pass NULL to above defined foo() (for example, for a situation when I don't need w[] be altered)? Given that the name of an array is a pointer to its first element, it seems to me that it should be OK, but perhpas I'm missing something?

谢谢!

推荐答案

这应该罚款。你传递的数组中元素的数量仅仅是形式主义,并没有真正需要的。 是W 只是一个指针 INT 。有没有其他运行时或编译检查。

It should be fine. The number of elements in the array that you passed are merely a formalism, and not really needed. w is just a pointer to int. There is no other runtime or compilation check.

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

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