pointers

Started by
11 comments, last by RaistlinMajere 20 years, 7 months ago
I typically use pointer to be able to dynamically change the size of my arrays. For example, I might declare a pointer to an int (int *MyVariable) Later, I might want to resize that array for some reason. I would write (MyVariable = new int[500]) And I would have an array of 500 ints. There are a lot of other uses for pointers, but that us probably my biggest use of them.
Peon
Advertisement
quote:Original post by Anonymous Poster
Why do people have to complicate pointers so much?

"Basically pointers are used to move around a sizable amount of data or create large data structures, such as a linked list or hash table."

First of all if the guy is asking "what are pointers" and you are going to get into hash tables then you shouldn''t start your sentence with the word "Basically".

Ok...for all of you who don''t know what a pointer is...

At the lowest level everything is 1''s and 0''s right? Well when you are programming we need to know what to do with those 1''s and 0''s. If you say "int x;" then the compiler knows that those bits that are represented by the variable x should be thought of as an integer right? Same goes for "char x" or "double x" or "float x" or any other of the built in types.

Well the creators of the C and C++ languages decided that they would provide a built in type that would represent memory address locations...and that type is a pointer. It "points" to a memory location.

When you do something like "int x" well the program needs to allocate memory for the variable x. And since it is in memory it will have a memory address right? Well if you want to store that memory address you could use a pointer like "int *y = &x" where the & means the address of x.

Thats all pointers are, WHAT they are has nothing to do with moving around large data structures. What they are GOOD FOR partly has to do with large data structures.



actually if you read a little harder , you''ll see he asked "what are pointers used for...?" not "what are pointers?"
those are two different questions.''
so my explanation is valid. thank you.

Beginner in Game Development?  Read here. And read here.

 

Aside from dynamic memory allocation, pointers are also used all the time with polymorphism. They provide a way to refer to many object types through a pointer to a common parent type allowing you to group functionality of objects which are related and share the same logic, yet whose implementations can vary. If you are referring to C and not C++, the same holds true, only you''d be working directly with vtables and vtable pointers.

This topic is closed to new replies.

Advertisement