2014-05-24 54 views
-3

让我举一个例子就明白了我的问题:指针在主函数中的修改而不指针

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 
} 

我要的是避免指针和只引用做到这一点,这是可能的?

+8

您*尝试*使用参考?什么地方出了错? (另外,请不要在[C]中标记有关引用的问题。在C中没有引用) –

+0

我正在使用visual C++,并且想使用引用,但我不知道如何? –

回答

1

使用引用是的,是可以做到的。

void fct2(int* &p) { 
    p++; 
} 
+0

这也会增加原始指针 – 4pie0

+1

是的。 OP想要那个。 –

+0

即使这是指针和参考不仅参考 – 4pie0

0

我会建议使用,如果可以通过std::array提供的迭代器:

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

template<class I> 
void fct2(I& it) 
{ 
    ++it; 
}