I have tried 2 ways to code length of an array one works the other not and I just don't understand what's the problem. First one works, secon not. This works:
#include<stdio.h>
int main(void)
{
char array[] = {'r', 'o', 'c', 'k', 'n', 'r', 'o', 'l', 'l'};
int length = sizeof(array) / sizeof(array[0]);
printf("Length is %d\n", length);
return 0;
}
This doesn't work, it always gives me 4.
#include<stdio.h>
unsigned int arraylen(const char *array)
{
unsigned int length = sizeof(array) / sizeof(array[0]);
return length;
}
int main(void)
{
const char array[] = {'r', 'o', 'c', 'k', 'n', 'r', 'o', 'l', 'l'};
unsigned int length = arraylen(array);
printf("Length is %d\n", length);
return 0;
}