Look carefully at your function declaration
void count(char* x, char* y){
^^^^^^^ ^^^^^^^
Its arguments have pointer types more precisely the type char *
.
So within the function expressions with the sizeof
operator like in these declarations
int x_Size = sizeof(x);
int y_Size = sizeof(y);
yield the size of pointers that in your system is equal to 8.
Also array designators used as function arguments as for example in this call
count(c1,c2);
are implicitly converted to pointers to their first elements.
On the other hand, in main in the expressions used in calls of printf
printf("size of main c1: %lu\n", sizeof(c1));
printf("size of main c2:%lu\n", sizeof(c2));
there are used arrays that when used in the sizeof
operator are not converted to pointers to their first elements. So you indeed get the sizes of the arrays in these calls.
From the C Standard (6.3.2.1 Lvalues, arrays, and function designators)
3 Except when it is the operand of the sizeof operator or the unary &
operator, or is a string literal used to initialize an array, an
expression that has type ‘‘array of type’’ is converted to an
expression with type ‘‘pointer to type’’ that points to the initial
element of the array object and is not an lvalue. If the array object
has register storage class, the behavior is undefined.
Within the function count
you could determine lengths of the stored strings in the passed arrays using the standard function strlen
. However sizes of arrays can differ from lengths of strings stored in the arrays.
In general if you want to pass an array to a function you need also to pass the number of elements in the array explicitly.