用于多级指针取消引用?
当在任何语言中使用指针需要有人使用多个指针时,比如说三重指针.什么时候使用三重指针而不是只使用常规指针有意义?
When does using pointers in any language require someone to use more than one, let's say a triple pointer. When does it make sense to use a triple pointer instead of just using a regular pointer?
例如:
char * * *ptr;
代替
char *ptr;
推荐答案
每颗星都应该读作一个指针指向的",所以
each star should be read as "which pointed to by a pointer" so
char *foo;
是由指针 foo 指向的字符".不过
is "char which pointed to by a pointer foo". However
char *** foo;
是由指向指针的指针指向的字符,而指针指向的指针指向指针 foo".因此 foo 是一个指针.在那个地址是第二个指针.在它指向的地址处是第三个指针.取消引用第三个指针会产生一个字符.如果仅此而已,就很难证明这一点.
is "char which pointed to by a pointer which is pointed to a pointer which is pointed to a pointer foo". Thus foo is a pointer. At that address is a second pointer. At the address pointed to by that is a third pointer. Dereferencing the third pointer results in a char. If that's all there is to it, its hard to make much of a case for that.
不过,仍有可能完成一些有用的工作.想象一下,我们正在编写 bash 或其他一些过程控制程序的替代品.我们希望以面向对象的方式管理我们的流程调用...
Its still possible to get some useful work done, though. Imagine we're writing a substitute for bash, or some other process control program. We want to manage our processes' invocations in an object oriented way...
struct invocation {
char* command; // command to invoke the subprocess
char* path; // path to executable
char** env; // environment variables passed to the subprocess
...
}
但我们想做一些奇特的事情.我们希望有一种方法可以浏览每个子进程所看到的所有不同的环境变量集.为此,我们将调用实例中的每组 env
成员收集到一个数组 env_list
中,并将其传递给处理它的函数:
But we want to do something fancy. We want to have a way to browse all of the different sets of environment variables as seen by each subprocess. to do that, we gather each set of env
members from the invocation instances into an array env_list
and pass it to the function that deals with that:
void browse_env(size_t envc, char*** env_list);
相关文章