Finding size of array passed as a parameter

In summary: So in summary, when passing an array to a function in C, the size is not automatically available and must be provided through another mechanism, such as a separate parameter or a designated end value within the array.
  • #1
Crystal037
167
7
Homework Statement
I want to find the size of an array passed as a parameter to a function let's say sort(int arr[])
Relevant Equations
I'm using this equation to find the size
int size=sizeof(arr)/sizeof(arr[0]);
C:
#include<stdio.h>
void sort(int arr[]){

int n=sizeof(arr)/sizeof(arr[0]);

printf("%d",n);}

void main(){

 int array[]={12,11,54,6,77};

sort(array);}
But I'm getting the answer as 2.
I searched it up and found out that array has decayed into pointer and hence its showing size of pointer which is 8 for 64-bit architecture and dividing it by 4 which is the size of int.
But this doesnt happen when this same equation is used inside main function.
Then how am I supposed to find the size of the array that has been passed to me as a function.

[Mentor Note -- added code tags]
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
C doesn't really have an array type. Instead it uses pointers. The declaration
Code:
 int a[] = {1,2,3,4}
actually means "store 1, 2, 3, and 4 in continguous slots in memory and set a to point to the first of those slots", and a[i] is the same as *(a + i).

C doesn't know - even in the function in which you declare it - how long an array is. It is your responsibility as programmer to keep track of that and check that you don't read past the end of the array. C won't throw an exception if you do that, it will just grab (or scribble over) whatever is in the next memory slot and you will wonder why your program starts producing nonsense or even crashes.

In C, passing an array to a function therefore requires passing two parameters: a pointer to the first element and the number of elements. You may be able to define a special value which, when encountered, tells the function that it has reached the end of the array, as strings do with the '\0' character, and the function will keep reading until it finds that value somewhere in memory. That value needs to be one which won't occur in valid data, and that isn't really possible here since any integer value might occur in an array to be sorted. So you are left with the 'pass the length' method.
 
  • #3
Crystal037 said:
I searched it up and found out that array has decayed into pointer and hence its showing size of pointer which is 8 for 64-bit architecture and dividing it by 4 which is the size of int.
Yes, that's correct. If you have defined the array outside of the function (say, in main()), then sizeof(a) will evaluate to the number of bytes allocated for the array, but as a parameter to a function, the name of the array "decays" to just a pointer. This happens whether the parameter type is int [] or * int, so as you note, sizeof a evaluates to the number of bytes allocated for the pointer itself.
 
  • #4
The size of an array passed to a function is C is not intrinsically available to the function. When the call is made, the array pointer is pushed onto the stack, loaded into a working register, or otherwise made available to the function.
If the function needs to know the size of the array, the programmer needs to provide this information through some other mechanism. For example, character strings are commonly terminated with a null value, variable record formats often include the size of the record in the first byte(s) of the array, and, of course, you can just add another parameter to the function.
 

1. How do I find the size of an array passed as a parameter in my code?

The size of an array can be found by using the .length property, which returns the number of elements in the array. This can be accessed by using the array variable followed by .length. For example, myArray.length will return the size of the array myArray.

2. Can the size of an array passed as a parameter change during runtime?

Yes, the size of an array can change during runtime. Arrays in most programming languages are dynamic, which means that elements can be added or removed from the array as needed. This will affect the size of the array, and the .length property will reflect the updated size.

3. What happens if I try to find the size of an empty array passed as a parameter?

If an empty array is passed as a parameter, the .length property will return a value of 0. This is because there are no elements in the array, so the size is 0.

4. Can I use a method other than .length to find the size of an array passed as a parameter?

Yes, some programming languages may have other methods or functions that can be used to find the size of an array. For example, in Java, the .size() method can be used to find the size of an arraylist. However, the .length property is the most commonly used method for finding the size of an array.

5. Is there a way to find the size of a multidimensional array passed as a parameter?

Yes, the .length property can also be used to find the size of a multidimensional array passed as a parameter. However, it will only return the size of the first dimension. To find the size of subsequent dimensions, you will need to use the .length property on each dimension separately.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
928
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Back
Top