指针的好处? [英] Benefits of pointers?

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

问题描述

我最近开发的C编程的兴趣,所以我给自己买了一本书(K&安培; R),并开始学习

I recently developed an interest in C programming so I got myself a book (K&R) and started studying.

在爪哇(基础知识)大学课程的到来,指针是一个完全新的篇章,并从我读在线这是一个相当困难的概念,围绕让你的头。让指针章之前我是IM pression的指针是C的重要组成部分,并提供了巨大的利益之下。

Coming from a University course in Java (basics), pointers are a totally new chapter, and from what I read online it's a rather difficult concept to get your head around. Before getting to the pointer chapter I was under the impression that pointers are a major part of C and provide great benefits.

在阅读的章节和得到什么指针是一个基本概念以及它们如何工作,带来的好处并不明显我的。

Upon reading the chapter and getting a basic idea of what pointers are and how they work, the benefits are not obvious to me.

(请纠正我,如果我得到这个完全错误的)中引入了K&放指针例如; R书它说,既然我们称之为按值,在函数调用传递一个变量时,我们pretty多通过该变量的副本处理功能,因此函数不能做任何事情到原来的变量,我们可以使用指针克服这一点。

For example (please correct me if I got this totally wrong) in the introduction of pointers in the K&R book it says that since we call by value, when passing a variable in a function call we pretty much pass a copy of the variable for the function to handle and therefore the function can't do anything to the original variable and we can overcome this with pointers.

在使用一个字符指针后面的例子,这本书说,递增字符指针是合法的,因为函数指针的私有副本。不是'传抄的理由来使用指针呢?

In a later example that uses a char pointer, the book says that incrementing the char pointer is legal since the function has a private copy of the pointer. Aren't 'private copies' a reason to use pointers instead?

我猜我有点困惑对整个使用指针。如果问我可以使用指针,而不是使用数组下标为例,但我怀疑这是主要的使用指针。

I guess I'm a bit confused on the whole use of pointers. If asked I can use pointers instead of using array subscripts for example, but I doubt this is the main use of pointers.

Linux和开源编程是主要的原因,我钻进C.我有一个C项目,研究(Geany IDE)的源$ C ​​$ c和我可以看到指针在整个源$ C ​​$ C使用

Linux and Open source programming was the main reason I got into C. I got the source code of a C project to study (Geany IDE) and I can see that pointers are used throughout the source code.

我也做了一些在论坛中的搜索和发现一对夫妇有类似的问题帖子。一个答案是(我引用):

I also did a bit of searching in the forums and a found a couple of posts with similar questions. An answer was (I quote):

如果你不知道什么时候你应该使用指针就是不使用它们。

当你需要使用它们将变得明显,每一种情况是不同的。

It will become apparent when you need to use them, every situation is different.

是否安全对我来说,避免在使用时存在的指针,只有在特定情况下(这里需要三分球将是显而易见?)

Is it safe for me to avoid using pointers at the time being and only use them in specific situations (where the need for pointers will be apparent?)

感谢您的任何输入,比较遗憾的是巨大的岗位:)

Thanks for any input and sorry for the huge post :)

推荐答案

您强调规则是非常明智的。这将让你摆脱困境,但迟早你要学会指针。

Your highlighted rule is very wise. It will keep you out of trouble but sooner or later you have to learn pointers.

那么,为什么我们要使用指针?

So why do we want to use pointers?

说我开了一个文本文件,并读入一个巨大的字符串。因为它太大,以适应堆栈(比如10MB)上,我不能按值传递你巨大的字符串。所以我告诉你的字符串是说去那边看看我的字符串。

Say I opened a textfile and read it into a giant string. I can't pass you the giant string by value because it's too big to fit on the stack (say 10mb). So I tell you where the string is and say "Go look over there at my string".

数组是一种指针(以及几乎)。

An array is a pointer ( well almost ).

INT []和INT *有微妙的不同,但是互换大部分。

int[] and int* are subtly different but interchangeable for the most part.

int[] i = new int[5]; // garbage data
int* j = new int[5]   // more garbage data but does the same thing
std::cout << i[3] == i + 3 * sizeof(int); // different syntax for the same thing

一个更高级的使用指针,这是非常有用的,是利用函数指针。在C和C ++函数不是第一类数据类型,但指针。所以,你可以将指针传递给一个函数,你想叫,他们可以这样做。

A more advanced use of pointers that's extremely useful is the use of function pointers. In C and C++ functions aren't first class data types, but pointers are. So you can pass a pointer to a function that you want called and they can do so.

希望帮助,但更可能会造成混淆。

Hopefully that helps but more than likely will be confusing.

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

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