I know that when you want to pass something by reference you are basically using pointers to point to the address of the data type. So when you want to pass an int or double by reference you put an asterisk after the data type in the function definition void example(int* a) and an ampersand in front of the data when you call the function in main example(&x) .
But I noticed when learning about sequences that when we want to pass a sequence by reference we need two asterisks, void example2(int** sequence) , and when you call the function in main you still only use one ampersand, example2(&seqOfInts) .
Is this just how the syntax is in c++ or is there a special reason why we need two asterisks when passing a sequence by reference?
What is the type of x?
What is the type of seqOfInts?
What you have and what you need determine the syntax.
PS. The use of term "by reference" is confusing, because funtion arguments can be either by value or by reference, and both of your example functions have by value arguments.
when you want to pass an int or double by reference you put an asterisk after the data type in the function definition void example(int* a) and an ampersand in front of the data when you call the function in main example(&x)
To really pass an int by reference, put an ampersand in the function definition void example(int& a) and nothing at the call site example(x). What you described creates a pointer that points to an int and passes that pointer by value, which is something commonly done in C.
idknuttin wrote:
I noticed when learning about sequences that when we want to pass a sequence by reference we need two asterisks
When we (c++ programmers) want to pass a sequence into a function, we use two iterators (or a range, or a view..): given std::vector<int> v;, the function call std::sort(v.begin(), v.end()); passes the entire contents of the sequence container v to the function std::sort as a pair of iterators.
What you're describing is a pointer that points to a pointer that points to an int, which is something used in C. To describe a pointer to a pointer to an int, you do indeed need two asterisks in the type name.
thank you, I just realized that what I was asking made no sense because I made a mistake on how passing by reference works but I now I understand after you cleared things up.