How can I find the number of elements in an array?

前端 未结 14 1146
广开言路
广开言路 2020-12-02 15:01

I have an int array and I need to find the number of elements in it. I know it has something to do with sizeof but I\'m not sure how to use it exac

相关标签:
14条回答
  • 2020-12-02 15:38

    In real we can't count how many elements are store in array

    But u can find the array length or size using sizeof operator.

    But why we can't find how many elements are present in my array.

    Because when we initialise a array compiler give memory on our program like a[10] (10 blocks of 4 size ) and every block has garbage value if we put some value in some index like a[0]=1,a[1]=2,a[3]=8; and other block has garbage value no one can tell which value Is garbage and which value is not garbage that's a reason we cannot calculate how many elements in a array. I hope this will help u to understand . Little concept

    0 讨论(0)
  • 2020-12-02 15:39

    i mostly found a easy way to execute the length of array inside a loop just like that

     int array[] = {10, 20, 30, 40};
     int i;
     for (i = 0; i < array[i]; i++) {
        printf("%d\n", array[i]);
     }
    
    0 讨论(0)
  • 2020-12-02 15:40

    I used following code as suggested above to evaluate number of elements in my 2-dimensional array:

    #include <stdio.h>
    #include <string.h>
    
    void main(void)
    {
        char strs[3][20] =
        {
            {"January"},
            {"February"},
            {""}
        };
    
        int arraysize = sizeof(strs)/sizeof(strs[0]);
    
        for (int i = 0; i < arraysize; i++)
        {
            printf("Month %d is: %s\n", i, strs[i]);
        }
    
    }
    

    It works nicely. As far as I know you can't mix up different data types in C arrays and also you should have the same size of all array elements (if I am right), therefore you can take advantage of that with this little trick:

    1. count number of bytes with sizeof() function from whole 2d array (in this case 3*20 = 60 bytes)
    2. count number of bytes with sizeof() function from first array element strs[0] (in this case 20 bytes)
    3. divide whole size with size of one element what will give you number of elements

    This snipped should be portable for 2d arrays in C however in other programming languages it could not work because you can use different data types within array with different sizes (like in JAVA).

    0 讨论(0)
  • 2020-12-02 15:40
    void numel(int array1[100][100])
    {
        int count=0;
        for(int i=0;i<100;i++)
        {
            for(int j=0;j<100;j++)
            {
                if(array1[i][j]!='\0') 
                {
                    count++;
                    //printf("\n%d-%d",array1[i][j],count);
                }
                else 
                    break;
            }
        }
        printf("Number of elements=%d",count);
    }
    int main()
    {   
        int r,arr[100][100]={0},c;
        printf("Enter the no. of rows: ");
        scanf("%d",&r);
        printf("\nEnter the no. of columns: ");
        scanf("%d",&c);
        printf("\nEnter the elements: ");
        for(int i=0;i<r;i++)
        {
            for(int j=0;j<c;j++)
            {
                scanf("%d",&arr[i][j]);
            }
        }
        numel(arr);
    }
    

    This shows the exact number of elements in matrix irrespective of the array size you mentioned while initilasing(IF that's what you meant)

    0 讨论(0)
  • 2020-12-02 15:45

    If we don't know the number of elements in the array and when the input is given by the user at the run time. Then we can write the code as

    C CODE:

    while(scanf("%d",&array[count])==1) { 
      count++;
    }
    

    C++ CODE:

    while(cin>>a[count]) {
      count++;
    }
    

    Now the count will be having the count of number of array elements which are entered.

    0 讨论(0)
  • 2020-12-02 15:52

    I personally think that sizeof(a) / sizeof(*a) looks cleaner.

    I also prefer to define it as a macro:

    #define NUM(a) (sizeof(a) / sizeof(*a))
    

    Then you can use it in for-loops, thusly:

    for (i = 0; i < NUM(a); i++)
    
    0 讨论(0)
提交回复
热议问题