Universal Counter for int or string array of pointers



  • Hello All!

    There is two array of pointer. First array of pointers on string and second on int.
    Is there any way to count elements in both array inside one function?
    I've tried to via arrays with counters but - no success.

    Thanks!



  • Maybe you can post an example so we know what's exactly your problem, just a small code 😉



  • I do not understand how size() work, and

    my custom counters fail:(

    #include <stdio.h>
    
    //with this function even not  compile
    /*
    int count_int(int *array){
        int i = 0;
    
        while(*(array+i)!=NULL){i++;}//warning: comparison between pointer and integer [enabled by default]
    
        return i;
    }
    */
    
    int main()
    {
        char *fruits[]={"Apple", "lemon", "Banana"};
        int i=0;
        int count=sizeof(*fruits);
    
        printf("%d\n",count); //result is 8 Why?
    
        int array[3] = {1,2,3};
        int count2=sizeof(*array); 
        printf("%d\n",count2); //result is 4 Why?
    
       // int n= count_int(array);
       //printf("%d\n",n); //result is 4 Why?
    
        return 0;
    }
    


  • LineZev schrieb:

    //result is 8 Why?

    *fruits is a pointer to char. Do you have a 64-Bit system?

    LineZev schrieb:

    //result is 4 Why?

    *array is an integer.

    int count_int(int *array[]){ //Array of Pointer
        int i = 0;
    
        while(*(array+i)!=NULL){i++;}
    
        return i;
    }
    
    int main()
    {
        char *fruits[]={"Apple", "lemon", "Banana", NULL}; // A NULL-terminated Array of Pointer
    
        int n= count_int(fruits);
    ...
    }
    

    Your array array is an array of int and no array of pointers



  • sizeof returns a size_t containing the size of the operator's argument in bytes.
    From the C11 standard draft N1570, $6.5.3.4.2:

    The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer.

    Important but irrelevant to your question the standard states this in the same paragraph:

    If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.

    That means a function printing a string will not print the string if invoked as an argument to sizeof.

    As you may know, an implicit conversion from T[] to T* (array to pointer) exists, that is, a pointer can "decay." The decayed pointer then points to the first element of the array.
    Dereferencing that pointer in turn yields the first element of the array.

    fruits's elemens are string literals and so is the first element. The expresion's type is char* (not char[6]!) here because the array is of type char*[4]. Therefore sizeof(*fruits) is equivalent to sizeof(char*), which apparently is 8 (or 64 bits) on your system.

    array's type is int[3], so sizeof(*array) == sizeof(int) == 4 (or 32 bits) on your machine.

    To detect an array's member count properly, see this: http://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c

    Regarding your count_int function: the call is undefined behavior. There is no element containing NULL, so the loop will cross array boundaries.
    Undefined behavior means the standard doesn't define any behavior for such a situtation. The fact that you obtain the value 4 is just an (un)lucky coincidence.



  • Besides, you attempt to compare an int with a NULL, thus the compiler issues the diagnostic message.



  • Thanks for Reply you'd really helps me.

    I found this and this respectively.

    I guess that there is no way to count elements in array of pointers on INT inside a function without custom struct, am I right?



  • No, there isn't because you can't delimit ints by anything. Every value could also be a valid int.

    However, you could agree on one value not usable by int members and used for terminating the array like 0xff. Then write a function similar to count_int but checking for 0xff instead of NULL.



  • Ok... I understand.

    Looks like It's working!

    Thank you very much!


Anmelden zum Antworten