为什么和何时值得使用指针指向指针? [英] Why and when is worth using pointer to pointer?

查看:91
本文介绍了为什么和何时值得使用指针指向指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何在C中使用指针?

Possible Duplicate:
How do pointer to pointers work in C?

你好

尽管如此,我认为我已经通过了编程的新手阶段,但是我仍然有一些问题,我无法以某种方式找到它们的解释.是的,那里有很多方法",但是几乎没有人解释为什么和/或何时使用一种技术.

Altough I think I passed the newbie phase in programming I still have some questions that somehow I can't find them explained. Yes, there are plenty of "how to"s overthere but almost always nobody explains why and/or when one technique is useful.

就我而言,我发现在某些情况下,C ++中使用了指向指针的指针.指向对象的指针不够吗?有哪些好处? 在什么地方或什么时候应该使用指向指针的指针?在这件事上,我感到有些迷茫.

In my case I have discovered that in some cases a pointer to a pointer is used in C++. Is not a pointer to an object enough? Which are the benefits? Where or when should be used a pointer to a pointer? I feel little bit desorientated in this matter.

我希望有经验的专家可以回答这个问题,希望其他没有经验的程序员也可以分享. ;-)

I hope time experienced expert could respond to this concerns that hopefully is shared by other no so experienced programers. ;-)

谢谢大家.

朱伦.

推荐答案

好吧,以某种方式很难回答这样一个普遍的问题.

Well, it is somehow hard to answer to such a general question.

C ++程序员的第一个答案肯定是:不要在C ++中使用指针!因为您有很多比指针更安全的方法来处理问题,所以您的目标之一就是首先避免出现问题:)

First answer of a C++ programmer will certainly be : Do not use pointers in C++ ! As you have a lot of safer ways to handle problems than pointers, one of your goal will be to avoid them in the first place :)

因此,在C ++中很少使用指向指针的指针.它们主要用在C中.首先,因为在C中,字符串是"char *",所以当您需要指向C字符串的指针"时,您必须以"char **"结尾.其次,由于您在C中没有引用,因此当您需要具有修改指针或将指针作为输出值的函数时,就需要为指针参数提供指针.通常,您会在分配内存的函数中发现这一点,因为它们会为您提供指向已分配内存的指针.

So pointers to pointers are seldom used in C++. They are mainly used in C. First, because in C, strings are "char*" so when you need a "pointer to a C string" you end with a "char**". Second, as you do not have references in C, when you need to have a function that modify a pointer or that give a pointer as an output value, you need to give a pointer to a pointer parameter. You typically find that in functions that allocate memory, as they give you a pointer to the allocated memory.

如果采用C ++方式,请尝试避免使用指针,通常会有更好的方式.

If you go the C++ way, try to avoid pointers, you usually have better ways.

my2c

这篇关于为什么和何时值得使用指针指向指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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