-3

Let me give an example to understand my question :

void fct1()
{
 int T[20];
 int* p=T;//the goal is to modify this pointer (p)
 fct2(&p);
}
void fct2(int** p)
{
  (*p)++;//this will increment the value of the original p in the fct1
}

What i want is to avoid pointers and do it only with references , it's possible ?

Taoufik J
  • 683
  • 1
  • 8
  • 23
  • 8
    Have you *tried* using a reference? What went wrong? (Also, please don't tag questions about references with [c]. There are no references in C.) – Cody Gray May 24 '14 at 15:10
  • I'm working with visual c++ , and i'd like to use references but i don't know how ? – Taoufik J May 24 '14 at 15:17

2 Answers2

1

Yes, it can be done using references.

void fct2(int* &p) {
    p++;
}
HelloWorld123456789
  • 5,221
  • 3
  • 20
  • 33
0

I would recommend using the iterators provided by std::array if possible:

void fct1()
{
    std::array<int, 20> l;
    auto it = l.begin();
    fct2(it);
}

template<class I>
void fct2(I& it)
{
    ++it;
}
David G
  • 90,891
  • 40
  • 158
  • 247