如何使用指针和malloc [英] how to use pointer and the malloc

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

问题描述

hai

我的任务是使用单个指针在链表中创建队列..

我想知道如何在指针struct下使用malloc ...

hai

my task is using a single pointer to creat a queue in linkedlist..

i want to know how to use malloc under pointer struct...

推荐答案

我不确定这是否真的适用于问题解答-它是其中一个看起来很简单,却需要灯泡"时刻的问题:突然之间都有意义的问题,您想知道为什么曾经觉得这很困难.不幸的是,指针和malloc是灯泡"的概念,因此,如果没有图片,不多的纸屑,每隔几分钟检查一次空白表情并在眼睛上闪耀,就很难解释.

Wiki可能会有所帮助:它涵盖了您需要了解的内容,但我怀疑它是由对它们了解得很好的人撰写的-可能对初学者而言太好了. http://en.wikipedia.org/wiki/Pointer_(computing) [
I''m not sure this is really suitable for a Q&A answer - it is one of those questions which looks simple, but takes a "lightbulb" moment: the ones where suddenly it all makes sense and you wonder why you ever thought it was hard. Unfortunately, pointers and malloc are "lightbulb" ideas, and as such are difficult to explain without pictures, loads of bits of paper, and a check every couple of minutes for a blank expression and glazed over eyes...

Wiki may help: it covers the stuff you need to know, but I suspect it was written by someone who understands them well - probably far too well for a beginner to follow. http://en.wikipedia.org/wiki/Pointer_(computing)[^]

In essence though, a pointer is a variable which tell you where something else is. A bit like the index in a book or a set of links in a web page: you look up the item you want to know about, and it tells you where it is. The index or list of links is not the item you are looking for, it is just a way of finding it.

Malloc returns the address of the object you asked for: the URL if you like. This gets saved in a pointer so that you can get to it again when you need it.

If you need more, try to read the wiki stuff. If that doesn''t help, talk to your tutor - at least he can see how confused you are getting!


这样想.

您有一本地址簿,里面有很多地址.这是您的一组指针.
许多东西可以在地址,工厂或房屋上,也可以只是一个简单的车库.

如果您分配一些数据,就好像买了一点土地,您可以将事情放到购买的土地的地址上.

用这些术语可以想到一个链表,您最初只有一个地址.
然后,当您购买或分配更多土地时,会将第二个地址的位置存储在他的第一个地址中.
我这样,您只需要知道第一个地址是什么时候,便可以找到第二个地址.

进行添加,您可以有很多地址,每个地址都包含链中的下一个地址.

希望这对您有所帮助.
Think of it like this .

You have an address book with lots of addresses in. This is your set of pointers.
Many things can be at an address , a factory or a house or just a simple garage.

If you malloc a bit of data it''s like buying a bit of land and you can put things at the address of the land that you bought.

A linked list can be thought of in these terms, you have a single address initally.
Then when you buy or malloc some more land you store the location of the second address at he first adress.
i this way you only need to know when the first adresses is and you can then find the second address.

Carry on adding and you can have lots of addresses each holding the next address in the chain.

Hopefully this helps I hope you have your light-bulb moment.


object* objectPointer = (object*)malloc(sizeof *objectPointer);
objectPointer->nextObject = NULL;


这样就可以得到一个地址保存在指针中的对象.将指针存储到该对象中的下一个对象(在上面放置了NULL),然后根据需要重复进行操作,您便拥有了链表.


That gets you one object whose address is saved in a pointer. Store a pointer to the next object in that one (where I put NULL above), and repeat as necessary and you have your linked list.


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

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