i am not sure why the value of b is coming out as 0
according to me it should come out as 3

#include <iostream>
using namespace std;
void read(int a[],int n)
{
    for (int k = 0;k < n;k++)
    {
        cout << "enter the " << k << "th integer";
        cin >> a[k];
    }
}
int main()
{
    int a[] = {};
    read(a,3);
    int b = sizeof(a)/sizeof(int);
    cout << b << endl;
    return 0;
}

sizeof is a compile-time 'function'... it cannot get the size of dynamic arrays.
So, your code is very wrong... your intention is to write 3 items into a, but there is only space for 0 items.

I compiled and ran it and was surprised it compiled and ran at all. Regardless, I think you're playing with fire memory-wise. I'm not sure what this does or why you can get away with it, but I think it's just luck. There must be some default array size somewhere and that's why it works for a small number like 3. I ran this program and as expected, it crashed for a large number like 50,000.

#include <iostream>
using namespace std;
void read(int a[],int n)
{
    for (int k = 0;k < n;k++)
    {
        a[k] = 1;
    }
}
int main()
{
    int a[] = {};
    read(a,50000);
    int b = sizeof(a)/sizeof(int);
    cout << b << endl;
    return 0;
}

You need to specify an array size in line 12 for this to work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.