Click here to Skip to main content
15,904,652 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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...
Posted

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!
 
Share this answer
 
Comments
Manfred Rudolf Bihy 21-Jun-11 12:23pm    
"lightbulb" moments: the ones where suddenly it all makes sense ...

Nice metaphor!
Good advice! 5
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.
 
Share this answer
 
object* objectPointer = (object*)malloc(sizeof *objectPointer);
objectPointer->nextObject = 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.
 
Share this answer
 
consider reading a beginner book like "C for Dummies" ;-)

in 1-2 years you will remember, "Why havent i done that?"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900