C++ c++;将指针地址传递给函数

C++ c++;将指针地址传递给函数,c++,dynamic-memory-allocation,calloc,C++,Dynamic Memory Allocation,Calloc,我想在将指针地址传递到此函数时更改函数内的数组值。 尝试写入阵列时,我收到一个运行时错误: Exception thrown at 0x002D1D65 in interviews.exe: 0xC0000005: Access violation writing location 0xCCCCCCCC. 我知道我可以用不同的方式来做,但这只是为了我的理解 代码如下: void func(int **p){ *p = (int*)calloc(3, sizeof(int)); //h

我想在将指针地址传递到此函数时更改函数内的数组值。 尝试写入阵列时,我收到一个运行时错误:

Exception thrown at 0x002D1D65 in interviews.exe: 0xC0000005: Access 
violation writing location 0xCCCCCCCC.
我知道我可以用不同的方式来做,但这只是为了我的理解

代码如下:

void func(int **p){
    *p = (int*)calloc(3, sizeof(int)); //have to stay like thiis
    *p[0] = 1;  //this line work fine but I think I assign the value 1 
                //to the address of the pointer
    *p[1] = 2;  //crashing here.
    *p[2] = 3;  
}
int main() {
    int* pm;       //have to stay like thiis
    func(&pm);     //have to stay like thiis
    int x =  pm[1];
    cout << x;
    return 0;
}
但它也会崩溃


我缺少什么?

[]
的优先级高于
*

*p[0] = 1; 
应该是

(*p)[0] = 1; 

与其他的代码> > *p>代码>发生。

或使用C++不仅通过标记和<代码> cOUT <代码> > <代码> Auto&ARR*P;arr[0]=1既然你使用C++,我建议使用<代码>新INT[3 ] < /C> >代替<代码> CaloC。你可以将< > > p p [ 1 ]改为P[0 ] [1 ]=1 < /Cord>。无论如何,C++方式是“代码> int *px= new int [3 ] {1,2,3};p=px。。。删除[]*p C++中的动态数组用<代码> STD::向量< /代码>拼写。
(*p)[0] = 1;