1

I want to understand how to know the size of array without using sizeof and not in main function. and i really want to understand why my code not working when i do it in other function.(in main it works same as sizeof). >>> The rasult i got are some trash numbers

*with char array is understood but i dont know how to do it with other data specifiers.

#include <stdio.h>

void arrSize(int a[],int b[]){
    int size_a = *(&a+1) - a;
    int size_b = *(&b+1) - b;
    printf("%d,%d",size_a,size_b);
}

int main(){
    int arr[] = {2,12,14,16};
    int brr[] = {8,53,2,4,16};
    
    arrSize(arr,brr);

    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
benny hassan
  • 101
  • 6
  • 4
    You can't know. You need to pass the size as an additional argument to the function. – user17732522 Jan 30 '22 at 14:08
  • 2
    In short, you can't. `int []` is literary an array of unknown size. The programmer is responsible to not read outside of the array. By passing another parameter to indicate the size or by other means. – Mary Chang Jan 30 '22 at 14:09
  • 2
    Does this answer your question? [Finding length of array inside a function](https://stackoverflow.com/questions/17590226/finding-length-of-array-inside-a-function) and [How do I determine the size of my array in C?](https://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c) (Read past the top answer as well!) – user17732522 Jan 30 '22 at 14:10
  • duplicates: [Finding length of array inside a function](https://stackoverflow.com/q/17590226/995714), [How to get array size within function?](https://stackoverflow.com/q/39429730/995714) – phuclv Jan 30 '22 at 14:14
  • Also, what do you mean by "size of array"? The number of elements or the number of bytes of storage used? – Jim Rhodes Jan 30 '22 at 14:20

1 Answers1

1

This function declaration

void arrSize(int a[],int b[]){
    int size_a = *(&a+1) - a;
    int size_b = *(&b+1) - b;
    printf("%d,%d",size_a,size_b);
}

is adjusted by the compiler to the following declaration

void arrSize(int *a,int *b){
    int size_a = *(&a+1) - a;
    int size_b = *(&b+1) - b;
    printf("%d,%d",size_a,size_b);
}

That is parameters having array types are adjusted by the compiler to pointers to array element types.

On the other hand, in this call

arrSize(arr,brr);

the arrays are implicitly converted to pointers to their first elements.

Having a pointer to the first element of an array you are unable to determine the array size pointed to by the pointer.

the initializers in these declarations

    int size_a = *(&a+1) - a;
    int size_b = *(&b+1) - b;

invoke undefined behavior because you are dereferencing pointers that do not point to valid object.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I guess you forgot to change square brackets with asterisk in the second version. – Costantino Grana Jan 30 '22 at 14:15
  • So theres is no way to determine the size of a given array outside the main function? – benny hassan Jan 30 '22 at 14:25
  • @bennyhassan If the array does not have a sentinel value then it is impossible. I do not take into account situations when a pointer to a whole array is passed like for example int a[5]; void f( int ( *p )[5] ); f( &a ); – Vlad from Moscow Jan 30 '22 at 14:36