如何使用预制数据初始化指向指针的指针?
我有一个指向指针的指针,因为我无法将动态数组传递给函数。但是,如果我想使用预制数据初始化该指针到指针,那么如何设置它,因为数组的 {a,b,c} 表示法不适用于指针?
I have a pointer to a pointer, since I can't pass dynamic arrays to functions. However, if I want to initialize that pointer-to-pointer with premade data, how can I set it since {a,b,c} notation for arrays won't work for pointers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以这样做:
此时,可以将
pptr
分配给int**
:You can do this:
At this point,
pptr
can be assigned to anint**
:[这个答案仅在您需要双*时才相关。您的问题已被编辑为指向指针的指针 - 如果这就是您所需要的,则此答案不相关。]
您可以这样做:
double fValues[3] = { 1, 2, 3 };
变量 fValues 已经是一个指针 - 不带 [] 的数组变量是指向数组第一个元素的指针。这不是动态数组,因此您不需要分配/释放其内存。
假设您的函数采用双指针,如下所示:
void Func(double* pDbl) {...}
您可以这样调用它:
Func(fValues);
代码>[This answer is only relevant if you need a double*. Your question was edited to say pointer to pointer - if that's what you need, this answer is not relevant.]
You can do this instead:
double fValues[3] = { 1, 2, 3 };
The variable fValues is already a pointer - array variables without the [] are pointers to the first element of the array. This is not a dynamic array, so you don't need to allocate/free its memory.
Assuming your function that takes a double pointer looks something like this:
void Func(double* pDbl) {...}
you'd call it like this:
Func(fValues);
您可以递归地创建小型动态数组,如下所示:
输出:
You can create small dynamic arrays recursively something like this:
Output: