1. Passing arrays to functions

This is a simple question from a C beginner.

When passing arrays as parameters to functions, do I always need to pass the array's length? Or is there a way to compute the arrays length inside the function?

Thanks.

2. You have to pass the length as another parameter.
Strictly speaking, a string is also an array. Through convention it is terminated by a '\0' character, so many of the library functions such as strlen() determine the array length by simply searching for this special terminator. I suppose you could do similar... if you had an array of ints, the terminating int might be a -1. Then inside the function you'd have a loop to search for a -1. As you can probably see, this would add computing time.

3. The book I am following only refers those two methods you specified. So I will take your word for it. And I will stop searching for other methods.

Thanks.

4. Originally Posted by engine
This is a simple question from a C beginner.

When passing arrays as parameters to functions, do I always need to pass the array's length? Or is there a way to compute the arrays length inside the function?

Thanks.
You can use "sizeof" as long as you've declared the size of the array you are passing into the function.

Here's a very simple example. In your prototype you leave the size of the array empty, when you are in the function you create an int variable and set it equal to the sizeof the array you just passed in + 1.

Code:
```#include <stdio.h>

void input(int[]);
void print(int[]);

void main()
{
int x[5];

input(x);
print(x);
}

void input(int a[])
{
int length = sizeof(a) + 1;
int i;

for(i=0; i < length; i++)
scanf("%d", &a[i]);
}

void print(int a[])
{
int length = sizeof(a) + 1;
int i;

for(i=0; i < length; i++)
printf("%d\n", a[i]);
}```

5. Originally Posted by FairyGuy
You can use "sizeof" as long as you've declared the size of the array you are passing into the function.
No you can't, your example only works because you lucked out.
Originally Posted by FairyGuy
Code:
```void main()
{
int x[5];

input(x);
print(x);
}

void input(int a[])
{
int length = sizeof(a) + 1;
int i;

for(i=0; i < length; i++)```
You luck out here, that's the only reason it works. Change the int x[5] to int x[50] and see what's wrong.

sizeof int = 4 in your case + 1 = 5 ... and why is it you think you need to add 1 anyway?

You can use sizeof on an array to get its size, but only if the array is declared in the same scope that you use it:
Code:
```int array[ 50 ];
int x = sizeof( array ) / sizeof( int );
printf( "array has %d members\n", x );```
You both need to read this.

Quzah.

6. Originally Posted by quzah
No you can't, your example only works because you lucked out.You luck out here, that's the only reason it works. Change the int x[5] to int x[50] and see what's wrong.

sizeof int = 4 in your case + 1 = 5 ... and why is it you think you need to add 1 anyway?

You can use sizeof on an array to get its size, but only if the array is declared in the same scope that you use it:
Code:
```int array[ 50 ];
int x = sizeof( array ) / sizeof( int );
printf( "array has %d members\n", x );```
You both need to read this.

Quzah.
Actually, sizeof(a) is equal to sizeof(int *) not sizeof(int). Arrays passed to functions decay to pointers.

7. Originally Posted by quzah
No you can't, your example only works because you lucked out.
I see your point, tried it with a larger number.
Originally Posted by quzah
You can use sizeof on an array to get its size, but only if the array is declared in the same scope that you use it:
Code:
```int array[ 50 ];
int x = sizeof( array ) / sizeof( int );
printf( "array has %d members\n", x );```
You both need to read this.
Quzah.
So in order to use sizeof in my example it would have to be in the main function, and like nonoob said passed as another parameter, which would be redundent if you already know the size of array you're declaring which leads me to ask is there any use in doing so?

8. Originally Posted by FairyGuy
So in order to use sizeof in my example it would have to be in the main function
It doesn't have to be in main. It has to be declared wherever you are using sizeof on it.,
Originally Posted by FairyGuy
and like nonoob said passed as another parameter, which would be redundent if you already know the size of array you're declaring which leads me to ask is there any use in doing so?
Good programming practices. Functions shouldn't need to rely on secret unknowns that are scattered throughout your code. They should only need to know about their parameters to do whatever they are supposed to do. Writing in the middle of your function the number 50, because you the programmer knew at the time of writing it that the array's size somewhere else was 50, is bad. If someone comes along and changes that array's 50 to 500, your function is now broken. Because it doesn't know you changed the array's 50, because its 50 is still 50.

"But I could just search-and-replace 50 with 500!"

Sure, if you want to do that every time you change anything. Oh, and if you want to assume that every 50 you talk about just happens to be the array size. But what if 50 is for something else? Now you've broken something else by trying to fix your array functions.

That's why.

Quzah.