Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

c++ - when do we need to pass the size of array as a parameter

I am a little bit confused about pass an array in C/C++. I saw some cases in which the signature is like this

void f(int arr[])

some is like this

void f(int arr[], int size)

Could anybody elaborate what's the difference and when and how to use it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First, an array passed to a function actually passes a pointer to the first element of the array, e.g., if you have

int a[] = { 1, 2, 3 };
f(a);

Then, f() gets &a[0] passed to it. So, when writing your function prototypes, the following are equivalent:

void f(int arr[]);
void f(int *arr);

This means that the size of the array is lost, and f(), in general, can't determine the size. (This is the reason I prefer void f(int *arr) form over void f(int arr[]).)

There are two cases where f() doesn't need the information, and in those two cases, it is OK to not have an extra parameter to it.

First, there is some special, agreed value in arr that both the caller and f() take to mean "the end". For example, one can agree that a value 0 means "Done".

Then one could write:

int a[] = { 1, 2, 3, 0 }; /* make sure there is a 0 at the end */
int result = f(a);

and define f() something like:

int f(int *a)
{
    size_t i;
    int result = 0;
    for (i=0; a[i]; ++i)  /* loop until we see a 0 */
        result += a[i];
    return result;
}

Obviously, the above scheme works only if both the caller and the callee agree to a convention, and follow it. An example is strlen() function in the C library. It calculates the length of a string by finding a 0. If you pass it something that doesn't have a 0 at the end, all bets are off, and you are in the undefined behavior territory.

The second case is when you don't really have an array. In this case, f() takes a pointer to an object (int in your example). So:

int change_me = 10;
f(&change_me);
printf("%d
", change_me);

with

void f(int *a)
{
    *a = 42;
}

is fine: f() is not operating on an array anyway.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...