Create a function to return the length of an int array in C

I'm new to c programming. I want to create a function that takes an integer array as argument and returns length of the array. I know that the code below calculates the length correctly.

    int arr[] = {1, 2, 3, 4, 5};
    int length = sizeof(arr) / sizeof(arr[0]);

But if I create a function like below and pass the array as an argument, it doesn't work.

    int length_of(int* arr) {
        return sizeof(arr) / sizeof(arr[0]);
    }

My guess is that I'm not passing the array into the function correctly. What is the correct way of implementing this?



Read more here: https://stackoverflow.com/questions/65714543/create-a-function-to-return-the-length-of-an-int-array-in-c

Content Attribution

This content was originally published by Atick Faisal at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: