简体   繁体   English

如何学习C指针?

[英]How to learn C pointers?

I have worked with pointers a lot, but still whenever I work with them, I feel that one might not work the way I expect it to. 我经常使用指针,但是每当我和它们一起工作时,我觉得可能不会像我期望的那样工作。 My mind is not sure as to how they will behave. 我的头脑不确定他们将如何表现。

What do you suggest? 你有什么建议? I want to learn pointers better. 我想更好地学习指针。

试试Binky

The best way is to write programs. 最好的方法是编写程序。 Get your hands on K&R, go through the basics and start writing some programs. 亲身体验K&R,完成基础知识并开始编写一些程序。
Sample problems could be : 样本问题可能是:
1) singly link list, doubly link list. 1)单链表,双链表。
2) Basic tree questions. 2)基本的树问题。
-- insert a value into a tree. - 将值插入树中。
-- find height, depth, delete a tree etc. - 找到高度,深度,删除树等。

I recommend reading "The C Programming Language" by Kernighan and Ritchie. 我建议阅读Kernighan和Ritchie的“The C Programming Language”。

Chapter 5 deals with pointers and arrays. 第5章讨论指针和数组。 This book is very clearly written, contains illustrations to demonstrate the concepts, and it also has exercises you can do to practice. 这本书写得很清楚,包含了演示概念的插图,还有练习你可以做的练习。 Even better, the book is short. 更好的是,这本书很简短。

Carefully perform all exercises you can find online -- this one , this one , etc (skip the C++ specific parts, such as "references", if you truly only care about C). 仔细执行你可以在网上找到的所有练习 - 这一个这一个等等(跳过C ++特定部分,如“参考”,如果你真的只关心C)。 Be sure to carefully study or rehearse each prereq material given in the exercise, if any. 如果有的话,请务必仔细研究或排练练习中给出的每个先决条件。 Once you're done, compare your solution code to the code given as the official solution -- do they both compile and work identically on the examples given? 完成后,将您的解决方案代码与作为官方解决方案提供的代码进行比较 - 他们是否在给出的示例中编译和工作相同? What about somewhat broader variants? 那些更广泛的变种怎么样? Any doubt you get, ask on stack overflow!-). 你有任何疑问,请问堆栈溢出! - )。

If you want to check how well you're doing, feel free to ask for more exercises if you haven't found enough online - simple but key ones such as "reverse a singly linked list in-place" or "remove a node from a singly linked list given a pointer to that node" are classics, but really getting them remains a great way to measure yourself on pointer issues. 如果你想检查自己的表现如何,如果你没有找到足够的在线内容,可以随时要求更多的练习 - 简单但关键的一些,例如“在现场反转单个链接列表”或“从中删除一个节点”给出指向该节点的指针的单链表“是经典的,但真正得到它们仍然是衡量指针问题的好方法。 (Single lists are great sources for difficult but important pointer exercises... that may be part of the reason why the still-current C++ standard only offers doubly linked lists, they just don't generate that many "interesting" problems!-). (单个列表是困难但重要的指针练习的重要来源......这可能是仍然当前的C ++标准仅提供链表的原因的一部分,它们只是不会产生那么多“有趣”的问题! - ) 。

In a nutshell - pointer are just a technique for indirection. 简而言之 - 指针只是一种间接技术。 For example, instead of passing a number directly (by value) to a routine, you can just pass the address of where the number is stored so that the routine that needs it can just look it up at the address. 例如,不是直接(通过值)将数字传递给例程,而是可以只传递存储数字的地址,以便需要它的例程可以在地址中查找。 Now this is interesting because whoever has that address, can change the value stored at that address - and all other holders of that address can see the results of that change. 现在这很有趣,因为拥有该地址的人可以更改存储在该地址的值 - 该地址的所有其他持有者都可以看到该更改的结果。

There can be a lot of messy syntax around pointers when doing pointer arithmetic, or pointer to functions, but the fundamental idea of a pointer is simple if you give yourself some time to contemplate it (and play with a couple of simple examples.) 在进行指针运算或指向函数的指针时,指针周围可能会有很多混乱的语法,但如果你给自己一些时间来考虑它,那么指针的基本思想很简单(并且可以使用几个简单的例子。)

Good luck, 祝好运,

Paul 保罗

I agree with Duleb, just write some code. 我同意Duleb,只是写一些代码。

But, to help you understand, draw out what is going on, as you may have an array of pointers, and you need to walk through, so you have pointers to pointers. 但是,为了帮助你理解,绘制出正在发生的事情,因为你可能有一个指针数组,你需要完成,所以你有指针指针。

By drawing diagrams you can visualize what is going on, and after a while you may find yourself drawing these images in your head, and it will make more sense. 通过绘制图表,您可以看到正在发生的事情,过了一段时间,您可能会发现自己在脑海中绘制这些图像,这将更有意义。

Unfortunately, until you get used to working with it pointers will probably be a mystery. 不幸的是,在你习惯使用它之前,指针可能是一个谜。 I don't think you can really understand how to use it by just reading a book. 我认为只要读一本书就不能真正理解如何使用它。

Try your hand at some assembly language. 尝试使用某种汇编语言。 This will show you really, really quickly what pointers actually are. 这将真正,非常快地向您展示实际指针。

Remember that at it's heart, all data manipulation, all of your data structures, all of your types, are just moving bits around or tracking them in memory. 请记住,在它的核心,所有数据操作,所有数据结构,所有类型,只是移动位或在内存中跟踪它们。

Understanding that an array of ints is simply a block of memory the size of 1 int times the number of elements in the array, and realizing that a struct doesn't have some magic delimiters or metabits floating in memory keeping the member variables apart, helps you to remember (realize?) the zen of C programing. 理解一个int数组只是一个内存块,大小为1 int乘以数组中元素的数量,并意识到struct没有一些魔术分隔符或代码浮动在内存中保持成员变量分开,有助于你要记住(实现?)C编程的禅。

Do you want to learn pointers better? 你想更好地学习指针吗? Learn the void pointer. 学习void指针。 It's the one without any bias, the purest form of a pointer, and think about the values you could realise from the same bits in memory. 它是没有任何偏见的那个,最纯粹的指针形式,并且考虑你可以从内存中的相同位实现的值。

It's all smoke and mirrors in the end - unlearn your hard types - and remember the bits in memory. 最后都是烟雾和镜子 - 忘掉你的硬类型 - 并记住内存中的各个部分。 The pointers are less adulterated than the ints or chars, and the void pointer is the purest of them all. 指针比int或chars更少掺杂,而void指针是最纯粹的指针。

I think it's something every programmer knows, but all too few realize. 我认为这是每个程序员都知道的事情,但很少有人意识到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM