Code 1:
#include <stdio.h>
int main () {
char *str = "Geeksquiz";
Char str1[] = "Geeksquiz";
Char str2[] = {' G ', ' e ', ' e ', ' K ', ' s ', ' Q ', ' u ', ' i ', ' Z '};
int n = sizeof (str)/sizeof (str[0]); 4, STR is a pointer
int n1 = sizeof (STR1)/sizeof (str1[0));//10, str1 is an array, note the ' i '
int n2 = sizeof (STR2)/sizeof (str2[ 0]); 9, Array,
printf ("n =%d, N1 =%d, N2 =%d", N, N1, N2);
return 0;
}
Notes:
in Main, the name of the ' array is ' is ' the size in bytes of the ' the ' array with sizeof.
However, an array decays to a pointer when passed to a function, so you get sizeof (int*) inside the function.
Code 2:
#include <stdio.h>
/* Array name is treated as a pointer when the function argument is passed
/void func1 (int a[]) {
printf ("%d\n", sizeof (a));//4
}
void Func2 (int *a) {
printf ("%d\n", sizeof (a));
}
int main () {
int array[] = {1, 2, 3};
printf ("%d\n", sizeof (array));
func1 (array);//4
Func2 (array);//4 return
0;
}
Reference: Http://stackoverflow.com/questions/9509829/c-size-of-array