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

前端 未结 14 1162
广开言路
广开言路 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: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

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

提交回复
热议问题