简体   繁体   English

关于指针的问题

[英]Questions about pointers

I have an API for a function that gets ***int as a parameter.我有一个将***int作为参数的函数的 API。

void APIfunc(int ***in_variable)

I write a new function and I have to use this APIfunc .我写了一个新函数,我必须使用这个APIfunc

I want to use a local int in my function (and not int*** ) but I want to pass it to the APIfunc (that gets int**** ).我想在我的函数中使用本地int (而不是int*** ),但我想将它传递给APIfunc (获取int**** )。

How can I do it?我该怎么做?

Relying only on syntax,仅依靠语法,

int foo;
int *bar = &foo;
int **bar2 = &bar;
APIfunc(&bar2);

However, just read function documentation.但是,只需阅读功能文档。 If is unbelievable that it takes so deep pointer chain just to modify single value;令人难以置信的是,仅仅修改单个值就需要如此深的指针链; must be something much more sophisticated.一定是更复杂的东西。

The way you call the function depends on whether it allocated memory or it just accesses the memory.调用函数的方式取决于它是分配内存还是只访问内存。

Case: It Allocates Memory案例:它分配内存

Since it does not seem to have arguments about the size of objects, the type of objects, number of objects, does it assume them?既然它似乎没有关于对象大小、对象类型、对象数量的参数,它是否假设它们? In that case, you need to call it thus:在这种情况下,您需要这样调用它:

int** in_variable;
APIfunc(&in_variable);

Case: It Just Accesses The Memory案例:它只是访问内存

You must have the data allocated somewhere.您必须在某处分配数据。 You need to call it thus:你需要这样称呼它:

int*** in_variable;
// Allocate memory and fill up the data
// ...

// Call the function
APIfunc(in_variable);

Hope that clarifies a few things for you.希望能为你澄清一些事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM