使用多级指针解引用? [英] Uses for multiple levels of pointer dereferences?

查看:119
本文介绍了使用多级指针解引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么时候使用任何语言的指针都需要有人使用多个,我们假设一个三重指针。

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;


推荐答案

每个明星都应该理解为一个指针so

each star should be read as "which pointed to by a pointer" so

char *foo;

是由指针foo指向的char。

is "char which pointed to by a pointer foo". However

char *** foo;

是由指向指向指针的指针指向的字符foo。因此foo是一个指针。在那个地址是第二个指针。在指向的地址是第三个指针。解除引用第三个指针将产生一个char。如果这是所有的事情,它很难做出大量的情况。

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);

这篇关于使用多级指针解引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆