1
main()
{
   int arr[] = {1, 2, 3, 4};
   function(arr);
}
int function(int a[])
{       

}

Here I want to get the length of the array which was not initialized. The main function in my case is which I do not have any idea..assume I am getting the array elements from some other program.

2

4 Answers 4

6

Arrays decay to pointers when used as arguments to a function:

int function(int a[]);

and

int function(int *a);

are the same.

This means that you cannot know how many elements are in an array that is passed to your function unless you have a separate parameter that indicates the length of the array.

Sign up to request clarification or add additional context in comments.

Comments

4

You can find out how many elements there are in arr in main() by sizeof(arr)/sizeof(arr[0]).

However, you cannot use this method in that function(). Because in this case a is a pointer to int, you need to pass the length of a as an extra argument to function(), like this:

int function(int a[], int len) {

Comments

1

You can't.

The function just receives a pointer (an address to a location in memory).

You have two options: pass the lenght of the array as an extra argument to the function (as other suggested)...

...or establish a convention (if possibile) where a certain value on the array represent the last element (so you can get the length by counting elements until the end-of-array item).

That's what happens with C strings. A C string is an array of chars where a character with value of 0 indicates the end of the string.

Comments

-2
#include<stdio.h>

int ArraySize (int *); 

main ()


{

   int array[] = {1,2,3}; 
   printf("The size of the array is%d \n", ArraySize(array)); 

}

int ArraySize (int *a)

{    

   for (int i=0; a[i]!='\0'; i++);
   return i;
} 

5 Comments

-1: in your ArraySize function the for control variable i ceases to exist at the end of the loop, before the return statement; and return, if this worked, would attempt to return an nonexistent variable. Also, the loop tries to access elements of the array that do not exist.
Correct me if I'm wrong, i will be incremented until the value a[i] equals Null (end of array). Name of array is passed to the function.
You are wrong, arrays are not automatically terminated by a null value. The function ArraySize() will go reading over the end of the array, return a wrong result (when will encounter a 0) and possibly read in a memory area the program have no access to resulting in a crash.
Do I need to terminate the array with null? e.g. int array[] ={1,2,3,NULL}. I'm new to C and trying to learn the ropes :)
@Abeer: You can't have values of different types in an array. 1, 2, -42, 1000 and so on are values of type int; NULL is a pointer value. You cannot mix pointers and integers. You could signal the end of the array with a 0, but then you couldn't have a real 0 as an element: int array[] = {-2, -1, 0, 1};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.