0

So my understanding of reference parameters is that they are used to point to the memory addresses of variables, therefore to modify the parameter in question.

ex:

void test(int x)  {
  x++;
}
int main()  {
  int number = 0;
  test(number);
  cout<<number;
  }

prints out 0 but

void test(int& x)  {
  x++;
}
int main()  {
  int number = 0;
  test(number);
  cout<<number;
  }

prints out 1.

However, with this function, I was able to modify the array parameter without &. Is this unique to arrays?

void genericF(int aC[], int n, int& total) {
    fstream info;
    string line;
    info.open("sample.txt");
    while (getline(info, line)) {
        if (stoi(line) < n) {
            total++;
            aC[stoi(line)]++;
        }
    }
}

where if I cast this function with another array, it changes the elements.

yungcoder
  • 123
  • 1
  • 2
  • 7
  • *they are used to point to the memory addresses of variables* not necessarily, though that is a common behind-the-scenes implementation. It is more correct to think of a reference as an alias, another name for, an existing variable. There is no reference variable, be cause the reference isn't a variable. It's just a name. When you use a reference as a function parameter, a smart compiler is allowed to reach out and directly use the referred-to variable, but often it can't and will sub in a sort-of pointer. – user4581301 Oct 30 '22 at 05:04
  • See [What is array to pointer decay?](https://stackoverflow.com/q/1461432/4581301) for what's going on with the array. Arrays are old magic from the 1970s. Back in those days you couldn't afford to pass an array by value because you had next-to-no RAM to store the copy in and you needed what little CPU power you could muster to do more important things. As a result C made it HARD to copy an array by value and made it easy to pass it by reference, and C++ inherited the same behaviour. If you want an array you can pass by value, use `std::array`. – user4581301 Oct 30 '22 at 05:09
  • @yungcoder See dupe: [Why is that you can modify an array inside a function without using any reference or pointer](https://stackoverflow.com/questions/71575718/why-is-that-you-can-modify-an-array-inside-a-function-without-using-any-referenc). – Jason Oct 30 '22 at 05:28
  • Forget that C-style arrays exist in the language and just use [std::array](https://en.cppreference.com/w/cpp/container/array) and [std::vector](https://en.cppreference.com/w/cpp/container/vector) exclusively. – Jesper Juhl Oct 30 '22 at 07:51

1 Answers1

0

Yes! In C++ just like in C, and array is simply a pointer, pointers are very similar to references, but a bit more complicated, they are exactly what you should learn in order to answer your question

Basically, a pointer is a number that holds the memory location of a variable, think of your memory as a huge array, that starts at cero and ends at the index of, well, the number of bytes of memory you have aviliable, then the pointer would represent the index of this hipotetical array where your variable is stored. In the case of an array, arrays are simply lists of values that are placed one after another in memory, so when you realize that you undestand that, for example, an array of ints is literaly the same thing as a pointer to an int. Since an array is secrely just a number that references a memory location, arrays are always passed around as memory references, and as such, as "passed by reference" (Pointers are the other way to pass a variable by reference in C++. The type of reference parameters you define like void function(int& x) are mostly an abstraction of pointers)

Keep in mind tho, that this is mostly pertinent to C, since in C++ you should NOT be using this type of arrays in most cases, instead you probably want to use and std::array (A more robust version of arrays that are introduced in the C++ standard library) or an std::vector (Like an array, but it's size can change at runtime). When you use them, you no longer have to think about them as pointers since you can now abstract them as just more generic collections of data, and you also don't have to deal with pointers directly (That gets very messy very quickly). Pointers are still being used behind the scenes, but you don't really have to worry about them

  • The only pointer in the above code is the decayed array. On reread, this seems to be because you skip the fist three quarters of the question and focus on the decayed array. – user4581301 Oct 30 '22 at 05:12
  • *Since an array is secrely just a number that references a memory location* You may wish to word this better. An array IS a block of memory. It's just usable as a pointer implicitly. – user4581301 Oct 30 '22 at 05:14