- #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);}
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: