C++ 将指针地址传递给函数
c++ pass pointer address to function
我想在将指针地址传递给该函数时更改函数内的数组值。 当我尝试写入数组时,我收到运行时错误:
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[1] = 2;
但它的崩溃也是如此。
我错过了什么?
[]
的优先级高于*
。
*p[0] = 1;
应该是
(*p)[0] = 1;
对其他人做同样的事情*p
发生。
相关文章:
- 将char*传递给pthread_read函数
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 我可以将调用类的"this"传递给 lambda 函数吗?
- 函数是否可以访问传递给main()的参数
- 将 ptr 传递给 ptr 到 A 作为参数传递给 A 的函数是不好的做法吗?
- 将函数作为模板参数传递给类
- 如何在 C++ 中将 void 函数传递给 void 指针
- 有没有办法按值将纯抽象类的所有子类传递给 C++ 中的函数?
- 将具有嵌入式单声道的 C++ 函数指针传递给 C#
- 将函数对象传递给 std::function
- 将函数指针传递给 C++ 中的成员函数.收到错误
- 如何将函数变量传递给C lambda函数
- 如何将 java 函数指针传递给 c++ 本机代码
- 如何将函数参数传递给boost::async()
- 将成员函数指针传递给 c 样式函数
- 将匿名函数对象传递给 std::function
- 与将标准作为模板参数传递给sort()相比,将标准函数指针传递给qsort()的开销更小
- 将C++函数指针传递给C函数
- 如何将函数参数传递给boost::thread_groups::create_thread()
- 将c++成员函数指针传递给STL算法