C语言 C 双指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5814646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C double pointer
提问by Jono
struct counter{
long long counter;
}
struct instruction{
struct counter *counter
int repetitions;
void (*work_fn)(long long *);
};
int ncounter; //number of counters
struct counter *counter; //counter array
int nthreads; //number of threads
int *ninstructions; //number of instructions
struct instruction **instructions;
How does this actually works ? I am having trouble with **pointers
这实际上是如何工作的?我在使用**指针时遇到问题
回答by Seth Carnegie
A **is just a pointer to a pointer. So where an instruction*contains the address of an instructionstruct, an instruction**contains the address of an instruction*that contains the address of an instructionobject.
A**只是一个指向指针的指针。因此,其中 aninstruction*包含instruction结构instruction**的地址,而 aninstruction*包含包含instruction对象地址的 an 的地址。
To access the instructionpointed to by the pointer pointed to by an instruction**, you just use two asterisks instead of one, like (**p).repetitionsor something similar.
要访问instructionan 指向的指针所指向的instruction**,您只需使用两个星号而不是一个,例如(**p).repetitions或类似的东西。
You can visualize it like this:
你可以像这样想象它:
instruction* ----> instruction
instruction** ----> instruction* ----> instruction
Remember, however, that simply declaring struct instruction** instructions;doesn't actually create an instructionstruct. It just creates a pointer that holds a garbage value. You'll have to initialize it:
但是请记住,简单的声明struct instruction** instructions;实际上并不会创建instruction结构体。它只是创建一个包含垃圾值的指针。你必须初始化它:
struct instruction inst;
// set members of inst...
*instructions = &inst;
...
(*instructions)->repetitions++; // or whatever
However, it looks like you're using an instruction**to point to an array of instruction*s. To initialize the array, you need a forloop:
但是,看起来您正在使用 aninstruction**指向一个instruction*s数组。要初始化数组,您需要一个for循环:
instructions = malloc(sizeof(struct instruction*) * num_of_arrays);
for (i = 0; i < num_of_arrays; ++i)
instructions[i] = malloc(sizeof(struct instruction) * size_of_each_subarray);
And then you can access an element like instructions[i]->datamember.
然后你可以访问像instructions[i]->datamember.
回答by littleadv
struct instruction **instructions; // How does this actually works ? I am having trouble with ** pointers
struct instruction **instructions; // How does this actually works ? I am having trouble with ** pointers
I'm not sure what the real issue is, but I'll try to answer the question.
我不确定真正的问题是什么,但我会尽力回答这个问题。
Double pointer is a pointer to pointer. It can be sued as array of pointers for example (if you allocate memory accordingly). For example:
双指针是指向指针的指针。例如,它可以用作指针数组(如果您相应地分配内存)。例如:
instructions = malloc(5*sizeof(struct instruction*));
for (int i = 0; i < 5; i++)
instructions[i] = malloc(sizeof(struct instruction));
And you got yourself nice array of 5 pointers to struct instruction. Use it like this:
你得到了 5 个指向struct instruction. 像这样使用它:
instructions[0]->repetitions = 0;
回答by Noufal Ibrahim
instructionsis a pointer to a pointer to struct instruction.
instructions是一个指向 的指针struct instruction。
This means that *instructionswill give you a pointer to a struct instruction. This kind of construct is often used to create a dynamic array of pointers to some compound type.
这意味着*instructions会给你一个指向 a 的指针struct instruction。这种构造通常用于创建指向某种复合类型的指针的动态数组。

