Finding size of array passed as a parameter

  • #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.
 

Similar threads

Replies
4
Views
1K
Replies
2
Views
2K
Replies
21
Views
2K
Replies
11
Views
2K
Replies
5
Views
2K
Replies
17
Views
2K
Replies
3
Views
1K
Back
Top