-1

i'm little stuck right now. I'm trying to find the size of an array in a function. for some reason i am getting a constant value of 8 for what ever size array. i'm trying the "sizeof()", and the array is char so the size of each char is 1. but when using size of in the the main i receive the correct size.

#include <stdio.h>

void count(char* x, char* y){
    int x_Size = sizeof(x);
    int y_Size = sizeof(y);
    printf("size of function x size:%lu\n", sizeof(x));
    printf("size of function y size:%lu\n", sizeof(y));
    for(int i = 0 ; i < sizeof(y)-1; i++){
        printf("%c", y[i]);
    }
}
int main(){
    char c1[] = "hello where are we?";
    char c2[] = "ye";

    printf("size of main c1: %lu\n", sizeof(c1));
    printf("size of main c2:%lu\n", sizeof(c2));
    count(c1,c2);

    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
piles fun
  • 11
  • 1
  • 2
    Does this answer your question? [Find the Size of integer array received as an argument to a function in c](https://stackoverflow.com/questions/25680014/find-the-size-of-integer-array-received-as-an-argument-to-a-function-in-c) – Smit Shah Feb 02 '21 at 18:57
  • 1
    You get 8 that is the size of a pointer in your system. On the other hand, if your inputs are always null terminated strings, you can just use `strlen ()`. – Roberto Caboni Feb 02 '21 at 19:05

1 Answers1

0

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335