0

I have a pretty basic c++ question (coming from a Python background).

I'm working through an example from the 'tour basics' book by Bjarne Stroustrup. Its basically this:


#include <iostream>

using namespace std;

struct Vector {
    int sz;         // number of elements in vector
    double* elem;   // pointer to elements in vector
};

void vector_init(Vector& v, int s)
{
    v.elem = new double[s];     // allocate an array of s doubles
    v.sz = s;
}

int main()
{

    Vector v;
    vector_init(v, 10);
    return 0;
};

What is causing me confusion is that the function vector_init takes a memory address of a vector for its first argument, but when its being used in the main, we are passing the vector v itself, not its memory address (which I assume would be &v rather than v).

This is working code and it compiles, but why do you not need to give the memory address as an argument here?

0 Answers0