Hello, everyone I am a student studying C language.
While solving the problem of creating a function that sorts the integers of an array in descending order, obtaining the length of the array from a function other than the main function results in a fixed value of 1.
I once heard that finding the length of an array with a size of operator is not good for other functions, so I looked through the book, but I can't see the contents. I'm asking because I can't know exactly even if I think about it and search
Below are the length calculation functions of the array with a value of 1 and the main function to run.
void arrlen(int arr[])
{
int len = sizeof(arr) / sizeof(int);
printf("%d", len);
}
int main(void)
{
int arr[10];
arrlen(arr);
return 0;
}
c sizeof
By default, if you declare a parameter in a function circle like this, It will be treated as an int* type.
The memory size of the int* data type always comes in 4 Bytes regardless of the size of the array, so dividing it by the size of the int data type of the same size inevitably results in 1.
It would be helpful if you refer to the link below. https://dojang.io/mod/page/view.php?id=562
© 2024 OneMinuteCode. All rights reserved.