So, I get the basic concepts of pointers, but what is the advantage of using them? What situation would I ever be in where pointers would be necessary?
Matt
So, I get the basic concepts of pointers, but what is the advantage of using them? What situation would I ever be in where pointers would be necessary?
Matt
Pointers serve three purposes:
1)To act as array iterators: Pointers can specify a position in an array. The semantics of pointers can at times be more convenient than using indexes. For instance, they make passing a range of array values simple.
2)To provide reference semantics. Often times, multiple abstract entities in a project need to be able to access the same data. Pointers provide the best way to do this in C; each abstract entity that needs to access and modify the information can be passed a pointer.
3)Pointers are vital in the use of dynamic memory. Dynamic memory, allows great control over when data is alive, and the size of a data block. Dynamic memory also has the advantage of being much bigger than the alternative static memory (that is, the Stack).
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
Think of it this way. Pointers are the way you tell a computer where stuff is.
In English, for you to tell me where to go to find a sunken treasure, you would give me the latitude, longitude and depth. For you to give me directions to Starbucks, you would tell me the street address. If I wanted you to text me, I would give you my cell phone number. We have lots and lots of different ways to tell each other where stuff is or how to send messages to each other.
A computer has pointers, which are addresses of memory locations. When you have a message in your program and you want to computer to issue the message, you must tell it where that message is, via a particular address, so it can obtain the string and move it to another address, and that address happens to be in a screen display buffer so the graphics hardware will pick it up.
Pointers are also used as locations of executable code.
In C, when you call a function, lots of times you will want to pass a pointer to data instead of passing the data itself. Think of this this way. If you had a broken down car and you wanted to take it to the shop, you can't drive it there (you can't pass the data - the car - to the function - the shop-), so you have to tell the mechanic that your car is on 5th and Elm Street - please fix it. He'll go to that address and fix it. In this case, you passed a pointer to the mechanic of where your car was.
Anyway, hopefully this will make some sense.
Last edited by Dino; 03-21-2008 at 10:53 AM. Reason: typos
Mainframe assembler programmer by trade. C coder when I can.
Ok, that helps. So with pointers, I can access data that isn't in the code? Like stuff just on my computer?
Put it this way, using pointers you can access multiple memory locations using the same name.
Code:>+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.
Oh, and those are separated by commas for proper syntax?
Argh, this is all so very well hidden in metaphor.
A pointer is a variable, and like all variables, it holds something as data; a pointer's data is a memory address. What can you use that for? All sorts of things, including but not limited to building parts of data structures, holding big chunks of memory, and returning multiple kinds of data from functions.
There you go. Three simple uses for pointers.
You can only hold one address per pointer, and it will be an address of a variable of the pointer's type. You can also declare multiple pointers on a line using commas like this:
Code:int *foo, *bar;
Oh, thanks. But you are contradicting the other guy
"using pointers you can access multiple memory locations using the same name."
Which is it?
Yes, I don't see how citizen is contradicting me.. Consider a pointer int *ptr and two variables int a,b;"using pointers you can access multiple memory locations using the same name."
As you can see you are using the same name ptr but accessing 2 different memory locations. This is particularly helpful when say you want to make changes to variables defined in one function in some other function.Code:int *ptr; int a,b; ptr=&a; *ptr=5; ptr=&b; *ptr=6;
Code:>+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.
At any point in time, a pointer variable can only point to one memory location. The pointer variable can be set with a different address later on, and thus it will point to something else. So, both are right, it's just PING was not clear.
Mainframe assembler programmer by trade. C coder when I can.
You cannot access variables defined in function x from function y because the scope of the variables in x is limited to that function.
Code:>+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.