Sunday, 29 April 2012

C++ Program to Count the Elements of an Array using Pointers till a Zero Element Appears


#include <iostream>

int a[10] = {4, 5, 8, 9, 8, 1, 0, 1, 9, 3};
int *ptra;

int main(  )
{
    ptra = a;

    while ((*ptra) != 0)
        ++ptra;

    std::cout << "Number of elements before zero " << 
        (ptra - a) << '\n';
    return (0);
}

No comments:

Post a Comment