0

I have written a function in c that gives me the wrong size of the array inside the function, but correct outside. Cant understand why..

My function:

    #define NELEMS(x)  (sizeof(x) / sizeof(x[0]))
    int check_val_is_valid(int32_t field_val, int value_array[], int32_t range_array[])
    {     
        printf("value_array is : %d\n", value_array);
        printf("Size of array: %zu\n", sizeof(value_array));
        size_t  size = 0;
        if (sizeof(value_array) >= sizeof(range_array)){
            size = NELEMS(value_array);
         }
        else{
            size = sizeof(range_array);
             printf("Size of array: %d\n", size);
        }
        printf("Size of array: %zu\n", size);
        int i=0;
        for(i=0; i < size; i++) 
       {
            printf("Value of i: %d\n", value_array[i]);
           if(value_array[i] == field_val) 
           {
                printf("\n%d", value_array[i]);
            
                return 1; 
            }
        };
        if (field_val < range_array[1] && field_val > range_array[0] )
        {
            return 1;
        }
    
        return 0; 
    }

Inside main:

        int val = 1000;
        int val_array[] = {1, 1000, 1234};
        printf(" array: %d\n",val_array);
        printf("Size of array: %d\n", sizeof(val_array));
        printf("Size of array: %d\n", NELEMS(val_array));
        int32_t rang_array[] = {0, 255};
        if (!check_val_is_valid(val, val_array, rang_array)){
            printf("The val is not valid!");
        };
    
        return 0;

From the terminal I get:

    array: 6356692
    Size of array: 12
    Size of array: 3
    value_array is : 6356692
    Size of array: 4
    Size of array: 1
    Value of i: 1
    The val is not valid!

Can't understand why the size of the array is different inside the function. How can i get the same size inside the function?

Marcusnh
  • 33
  • 8
  • You do not have a variable of an array type in your function. You have pointers tthat point to first elements of arrays. – Vlad from Moscow Jan 08 '21 at 17:05
  • ...even if you define the argument as `int value_array[3]` there is still no array, and the compiler ignores the `3` and even *then* `sizeof` won't give you what you expected. – Weather Vane Jan 08 '21 at 17:24
  • okay so how do you take in an array and get the correct sizeof? Only solution I have found is to take in the size as an argument. – Marcusnh Jan 08 '21 at 17:38
  • @MarcusNotø "how do you take in an array and get the correct sizeof" --> `sizeof some_array`. But with `check_val_is_valid(int32_t field_val, int value_array[], int32_t range_array[])`, `field_val` is a pointer, not an array. Instead of `main()` passing in `val`, code could pass in the address of `val` and then the receiving function needs a VLA pointer prototype. Consider instead passing to `check_val_is_valid()`, `val` and its element count as 2 arguments. – chux - Reinstate Monica Jan 08 '21 at 23:27
  • when asking for "how to get the correct size" i meant inside the function. Don't see how that will improve the code. Don't see how using the reference of '''val''' will make it possible to get the correct size of the array. – Marcusnh Jan 09 '21 at 14:25

0 Answers0