为什么和何时值得使用指针指向指针?

编程入门 行业动态 更新时间:2024-10-24 06:35:02
本文介绍了为什么和何时值得使用指针指向指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可能重复: 如何在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

更多推荐

为什么和何时值得使用指针指向指针?

本文发布于:2023-06-01 10:27:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/412984.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!