-1

Why this program couldn't run the 'printArray' function. The program is running perfectly up to the function call. I hope you can help, thanks in advance!

#include <iostream>
using namespace std;

struct Point
{
    char name[10];
    int age;
};

void printArr(Point ar[])
{
    for (int i = 0; i < sizeof(ar) / sizeof(ar[0]); i++)
    {
        cout << "Name of " << (i + 1) << " person: " << ar[i].name;
        cout << "Age of " << (i + 1) << " person: " << ar[i].age;
    }
}

int main()
{
    struct Point p;
    int n;
    do
    {
        cout << "Enter n: ";
        cin >> n;
        if (n <= 0)
            cout << "Ama ti si bil mnogo tup da vuvedesh n<=0\n";
    } while (n <= 0);
    struct Point ar[n];

    for (int i = 0; i < sizeof(ar) / sizeof(ar[0]); i++)
    {
        cout << "Enter name of " << (i + 1) << " person: ";
        cin >> ar[i].name;
        cout << "Enter the age of " << (i + 1) << " person: ";
        cin >> ar[i].age;
        cout << "\n";
    }
    cout << "Now we have to run the 'printArr' function!\n";
    printArr(ar);

    return 0;
}
Marek R
  • 32,568
  • 6
  • 55
  • 140
Kaloyan D
  • 1
  • 1
  • 1
    https://godbolt.org/z/nhrGTjTs8 – Marek R Feb 22 '22 at 17:11
  • 4
    The compiler interprets a function parameter like: `Point ar[]` as being equivalent to "Point *ar`. So when you do `sizeof(arr)` inside the function, you're getting the size of a pointer, not the size of the array. To make it work, you could pass the array by reference, pass the size explicitly, or (the usually-preferred choice) use an `std::vector`. – Jerry Coffin Feb 22 '22 at 17:16
  • 1
    Use `std::vector` https://godbolt.org/z/4Yhxcaooa – Marek R Feb 22 '22 at 17:18

1 Answers1

3

When you wrote:

void printArr(Point ar[]) //parameter ar is actually Point*
{
    //...
}

In the above function declaration(which is also a definition), the function parameter ar is actually a Point* instead of an array of Point objects. So sizeof(ar) gives us the size of the pointer instead of the array.

Additionally, in standard C++ the size of an array must be a compile time constant. So when you wrote:

int n;
cin >> n;
struct Point ar[n]; //NOT STANDARD C++

The statement struct Point ar[n]; is not standard c++ because n is not a constant expression.

A better way to avoid these problems would be to use a std::vector as shown below:

#include <iostream>
#include <vector>

struct Point
{
    char name[10];
    int age;
};

void printArr(const std::vector<Point>& ar) //takes argument of type vector<Point> by reference
{
    for (int i = 0; i < ar.size(); i++)//use size() member function of std::vector
    {
        std::cout << "Name of " << (i + 1) << " person: " << ar[i].name<<"\n";
        std::cout << "Age of " << (i + 1) << " person: " << ar[i].age<<"\n";
    }
}

int main()
{
    struct Point p;
    int n;
    do
    {
        std::cout << "Enter n: ";
        std::cin >> n;
        if (n <= 0)
            std::cout << "Ama ti si bil mnogo tup da vuvedesh n<=0\n";
    } while (n <= 0);
    //create vector of Point of size n instead of array of Point of size n
    std::vector<Point> ar(n);

    for (int i = 0; i < ar.size(); i++)//use size() member function 
    {
        std::cout << "Enter name of " << (i + 1) << " person: ";
        std::cin >> ar[i].name;
        std::cout << "Enter the age of " << (i + 1) << " person: ";
        std::cin >> ar[i].age;
        std::cout << "\n";
    }
    std::cout << "Now we have to run the 'printArr' function!\n";
    printArr(ar);

    return 0;
}

The output of the above program can be seen here.

Jason
  • 36,170
  • 5
  • 26
  • 60