0
#include<iostream>
#include <vector>

using namespace std;

int size_f(vector<int> adj[])
{
    return sizeof(adj);
}

int main()
{
    vector<int> adj[] = {{0, 1}, {0, 2}, {1, 2}, {2, 3}};

    cout << sizeof(adj) << endl;

    cout << size_f(adj);
}
96
8

This is my program and its result. I don't understand why size_f return 8. Can you explain it for me? And how can I know size of array that is parameter of my funtion. Thank you.

dat doan
  • 15
  • 3
  • `8` is returned because the size of pointers in your system is 8. Pass the size of array as another parameter to have functions know the size of array. – MikeCAT Jun 16 '21 at 16:47
  • When declaring an argument using "array" syntax like you do, then the compiler will treat it as a pointer. In other words, as an argument the declaration `vector adj[]` is exactly the same as `vector *adj`. Perhaos you should use a vector of vectors? Or `std::array, 4>`? – Some programmer dude Jun 16 '21 at 16:50

0 Answers0