-1

I'm very amateur when it comes to pointers. What I'm trying to do is I want to create a function which takes parameter as an array or vector and modifies their actual reference values. But I don't want to directly pass a vector as the parameter. I want to pass some variables clustered together as a vector:

void myFunction (vector <int> &a)
{
    for (int i = 0; i < a.size(); i++)
    a[i] = i;
}

int main()
{
    int a = 5, b = 6, c = 8;
    myFunction ({&a, &b, &c}); // This line shows the error. Help me to repair it.
    cout << a << " " << b << " " << c << endl;
}

I tried googling and mixing &, * and const in my code but nothing helped.

Edit: All I had to do was add a * in <int> and a const afterwards:

void myFunction (vector <int *> const &a)
{
    for (int i = 0; i < a.size(); i++)
    a[i] = i;
}

int main()
{
    int a = 5, b = 6, c = 8;
    myFunction ({&a, &b, &c}); // This fixed the error which was showing here.
    cout << a << " " << b << " " << c << endl;
}

Thanks to the person in comments who asked me to put the * after int. I'm very beginner so I absolutely needed help with that. Unfortunately, this community looks pretty harsh to beginners.

  • 1
    if the function is `void myFunction (vector &a)` then you can only call it by passing a vector of `int` – 463035818_is_not_an_ai Apr 17 '23 at 14:29
  • 1
    ***I tried googling and mixing &, * and const in my code but nothing helped.*** Using either of those should not help. What are you really trying to do? – drescherjm Apr 17 '23 at 14:30
  • 1
    You could use a `std::vector>` but this smells like an XY problem. What actual issue/problem are you trying to solve? We might be able to help you come up with a better way. – NathanOliver Apr 17 '23 at 14:30
  • `myFunction` is bad symbol name. What this function suppose to do? – Marek R Apr 17 '23 at 14:30
  • 2
    pointers are overrated. You expect them to be magic, but they are not. Your function expects parameter of certain type, you try to pass something different, thats not going to work. – 463035818_is_not_an_ai Apr 17 '23 at 14:30
  • 1
    Googling C++ syntax is tricky. Instead use the English terminology. How do you learn that terminology? Generally from [good books](https://stackoverflow.com/q/388242/4581301). – user4581301 Apr 17 '23 at 14:30
  • How do I make it accept pointers to `int`s? – THE BLUE FRIEND Apr 17 '23 at 14:31
  • 3
    This is [XY problem](http://mywiki.wooledge.org/XyProblem). Please explain what code should do! Looks like you are trying do something simple in some strange convoluted way. – Marek R Apr 17 '23 at 14:33
  • 2
    "This community looks pretty harsh to beginners" - it's not easy to learn a programming language, or to ask a good question. But it's not easy to teach someone basic concepts in a Q&A format either, and it scales very poorly. – Useless Apr 17 '23 at 15:03
  • It's difficult especially for a beginner to write a good question for the Q&A. Remember that StackOverflow is not a user forum at all. The #1 purpose of a question is how much it can help teach future readers or solve their future similar problem. – drescherjm Apr 17 '23 at 18:57

2 Answers2

0

Remember that elements in an array (and, because a vector uses an array internally, in a vector) are each proper objects. Defining single variables like a, b, c etc. and putting pointers to them in a vector is redundant: Why do you need the storage outside the vector at all? This is all the more true because you pass a reference to the vector to your function.1

(As an aside: What would you do if the vector had 100 elements, or 10000!?)

Instead, you can simply use a vector right away and rely on the fact that each element is already a true int object, like a variable:

#include <iostream>
#include <vector>
using namespace std;

/**
 * @brief Modify the vector to which the reference refers
 * @param vecRef 
 */
void fillConsecutive(vector <int>& vecRef)
{
  for (int i = 0; i < vecRef.size(); i++)
  vecRef[i] = i;
}

int main()
{
  vector<int> data{ 10, 100, 1'000, 10'000 }; // or fill it any other way

  // Modify the original vector.
  fillConsecutive(data); // This line showed the error ;-)

  for (auto i: data)
  {
    cout << i << '\n';
  }
}

1If, instead, you would copy the entire vector, you would create a second set of elements which are independent of the original ones, and modifying them in the function would not change the originals in the other vector outside of it. In that case you would indeed have to use pointers because the copied addresses in the new vector will still point to the original variables; they contain the same addresses as the elements of the vector outside the function, from which they were copied.

But passing the vector by value (i.e., passing a true, deep copy) would be untypical and is almost surely not what you want. You pass the vector by reference, which is typical, and can readily modify the original object through that reference. Remember: A reference is essentially an alias, another name, for the same object. Nothing is copied when you call the function.

Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62
-1

In this line:

 myFunction ({&a, &b, &c}); // This line shows the error. Help me to repair it.

The argument passed to myFunction is not a vector, but a standard array. Try this instead:

#include<iostream>
#include <vector>
using namespace std;

void myFunction (vector <int> &a)
{
    for (int i = 0; i < a.size(); i++)
    a[i] = i;
}

int main()
{
    int a = 5, b = 6, c = 8;
    vector<int>myVector{a,b,c};
    myFunction (myVector); // This line shows the error. Help me to repair it.
    cout << a << " " << b << " " << c << endl;
}
stew
  • 1
  • 4