0
#include <stdio.h>
#define N 5

int length(int b[]) {
    return sizeof(b)/sizeof(b[0]);
}
int main(void) {
    int a[N] = {1, 2, 3, 4 , 5};
    printf("%d\n", length(a));
}

I've played around with this a bit and just wondering why it doesn't print the length of the array a. I know other ways to print the length, but I'm just wondering what the specific issue of this block of code is.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 3
    `sizeof(b)` is the size of a pointer as with parameter `int b[]`, `b` is a _pointer_. Was that intended? – chux - Reinstate Monica Dec 12 '22 at 01:38
  • 3
    [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) has several explanations of why you can't use `sizeof` to determine the array size once it has decayed to a pointer. – Retired Ninja Dec 12 '22 at 01:39

0 Answers0