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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
vijay kumar kdp
  • 37
  • 1
  • 10
  • pass extra argument, or use strcut, or append one more information to array = length of array. – Grijesh Chauhan Apr 16 '14 at 13:42
  • possible duplicate of [How to find the 'sizeof'(a pointer pointing to an array)?](http://stackoverflow.com/questions/492384/how-to-find-the-sizeofa-pointer-pointing-to-an-array) – Lundin Apr 16 '14 at 14:02

4 Answers4

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.

turbulencetoo
  • 3,447
  • 1
  • 27
  • 50
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) {
Lee Duhem
  • 14,695
  • 3
  • 29
  • 47
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.

Paolo
  • 15,233
  • 27
  • 70
  • 91
-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;
} 
Abeer
  • 5
  • 4
  • -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. – pmg Apr 16 '14 at 15:31
  • 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. – Abeer Apr 16 '14 at 16:15
  • 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. – Paolo Apr 16 '14 at 16:33
  • 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 Apr 16 '14 at 17:17
  • @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};`* – pmg Apr 16 '14 at 17:25