🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

the point of pointers

Started by
13 comments, last by webspynner_99 24 years, 5 months ago
I''m just learning C and would like to know the reason for using pointers. Why change the data stored in a variable by using the memory address of the variable (indirect) when you can just change the variable directly? Example: int x, *pnt_x; x=''h''; pnt_x=&x *pnt_x=''i''; this, to me, seems exactly the same as: int x; x=''h''; x=''i''; Are there certain advantages to accessing a variable indirectly? I can see that instead of changing the variable, what you''re actually doing is change the data at the location reserved by the variable. But once again, isn''t it all the same?
Advertisement
Pointers are most helpful when you start dealing with structures and functions. It would take a lot of time and extra memory to pass a structure to a function whereas if you have a pointer to a structure and then just past the pointer to the function it takes less time and memory. That is the biggest advantage to pointers.
Pointers come in REAL handy with a number of things. One of the main ones is the ability to change the original value from inside a completely different function. As you know functions can only return one value, but by passing pointers, your possibilities are endless.
Pointers also give you the memory addresses of hardware. DirectX is completely built on pointers.

Best of all, pointers are great for reorganizing data. If you had like a hundred values that you needed to organize from highest to lowest, it''s TONS faster to simply reorganize the pointers rather than the actual data.

I guess what it all comes down to is this: yeah, pointers are confusing as hell at first, and in most cases you could probably get away without using them. But with pointers, you open up a whole new door of opportunity...especially when your programs start to get massive in size.
I''m not the world''s expert on pointers, and I''m sure that someone more knowledgable (?) will jump right up and tell you, but the real power of pointers comes when you have two variables. In your sample, you only had one int--x. With the *pnt_x variable, you can access any integer. There are millions of things you can do with pointers (my favorite are linked lists). In fact, if it wasn''t for pointers, I''d probably write only VB...

2mikes.com

FEE
Pointers are the only way you can alter dynmaic memory.

Domini
without pointers their wouldnt be com or polymorphism
-PoesRaven
let''s put it this way. without pointers, there wouldn''t be very much at all.

webspynner_99, keep doing the examples in your book (or tutorial) on pointers, keep at it even though it might not be very clear, and one day you''ll "see the light" and you''ll understand pointers, and you''ll wonder why you didnt understand them before, because theyre so logical. At least thats how I learned the real use of pointers. it just "clicked" after a few hours of confusion.

pointers give you an ability above and beyond the normal "a = b;" assignment of variables. it''s like a whole new level of control (or lack thereof!)

thats my story and im sticking to it
I understand how you feel webspynner_99. When I was learning how to program, I had the exact same thought. While many of the descriptions above are correct, I don''t know if they are clear enough for a beginner.

Here''s an example that I think will help. Say you want to write a simple program that let''s the user input some numbers, then you are going to sort the numbers.

Well, you have no way of knowing how many numbers the user is going to want to put in. He may want 10 or 1000. You could just put a really big array in your program, but that just isn''t a good idea, and besides, you couldn''t be certain that the user wouldn''t want to put im more numbers than your array size.

The solution is pointers. By putting a pointer into your program, you can allocate all the memory you need (by asking the user how many numbers he is going to put in), then point that pointer to the new memory. Now when you access the pointer, it will point to the first number in the new array.

Of course, pointers have many more uses than this, and you''ll learn them in due time. As far as the example you showed, this is just a way of teaching you about pointers. In real life, you are correct. Using pointers this way is pretty much useless.

Yeah, I agree with foo and Shaolin. When I was first learning C++, I didn''t understand pointers. They were sort of confusing, and I couldn''t understand why anybody would want to use them. But after a while, I just realized that I had gotten the point finally, and now I don''t know how I ever lived without them.
pointers have many uses which couldnt be done as fast.

here''s one example... say you have a data structure which takes up 4MB for each struct and you need to pass one of these data structures to a function do manipulate it. each time this function was called, the entire 4MB would have to be passed. it would all have to be placed on the stack too (i think.. it''s been a while since i dealt with low-level junk. if you used pointers, you could just pass the pointer which points to the start of the data structure. since pointers are 32bit values on 32bit systems, you only need to pass 4bytes instead of 4megabytes. would this be 1 million times faster?
Carl "trixter"[email=carl@trixoft.com]carl@trixoft.com[/email]http://www.trixoft.com

This topic is closed to new replies.

Advertisement