Uses for multiple levels of pointer dereferences?(用于多级指针取消引用?)
问题描述
当在任何语言中使用指针需要有人使用多个指针时,比如说三重指针.什么时候使用三重指针而不是只使用常规指针有意义?
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);
这篇关于用于多级指针取消引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:用于多级指针取消引用?
基础教程推荐
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07