Jump to content
    1. Welcome to GTAForums!

    1. GTANet.com

    1. GTA Online

      1. Los Santos Drug Wars
      2. Updates
      3. Find Lobbies & Players
      4. Guides & Strategies
      5. Vehicles
      6. Content Creator
      7. Help & Support
    2. Red Dead Online

      1. Blood Money
      2. Frontier Pursuits
      3. Find Lobbies & Outlaws
      4. Help & Support
    3. Crews

    1. Grand Theft Auto Series

      1. Bugs*
      2. St. Andrews Cathedral
    2. GTA VI

    3. GTA V

      1. Guides & Strategies
      2. Help & Support
    4. GTA IV

      1. The Lost and Damned
      2. The Ballad of Gay Tony
      3. Guides & Strategies
      4. Help & Support
    5. GTA San Andreas

      1. Classic GTA SA
      2. Guides & Strategies
      3. Help & Support
    6. GTA Vice City

      1. Classic GTA VC
      2. Guides & Strategies
      3. Help & Support
    7. GTA III

      1. Classic GTA III
      2. Guides & Strategies
      3. Help & Support
    8. Portable Games

      1. GTA Chinatown Wars
      2. GTA Vice City Stories
      3. GTA Liberty City Stories
    9. Top-Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    1. Red Dead Redemption 2

      1. PC
      2. Help & Support
    2. Red Dead Redemption

    1. GTA Mods

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Red Dead Mods

      1. Documentation
    3. Mod Showroom

      1. Scripts & Plugins
      2. Maps
      3. Total Conversions
      4. Vehicles
      5. Textures
      6. Characters
      7. Tools
      8. Other
      9. Workshop
    4. Featured Mods

      1. Design Your Own Mission
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Rockstar Games

    2. Rockstar Collectors

    1. Off-Topic

      1. General Chat
      2. Gaming
      3. Technology
      4. Movies & TV
      5. Music
      6. Sports
      7. Vehicles
    2. Expression

      1. Graphics / Visual Arts
      2. GFX Requests & Tutorials
      3. Writers' Discussion
      4. Debates & Discussion
    1. Announcements

    2. Support

    3. Suggestions

Pointers


Tornado Rex
 Share

Recommended Posts

Ok, so we're working with pointers in class. Now, I get what pointers do, but what I can't seem to figure out is what the practical purpose behind them is. Maybe it's just because we've only been doing stuff where the pointer could be replaced with a normal variable.

 

So anyway, what are some practical uses behind pointers?

Link to comment
Share on other sites

I don't know sh*t about C++ or pointers, but I know the answer to your question is arrays.

"You can play faster than Al Di Meola and do it with only one pinky, but if you're not listening to what is going on around you, you might as well just shut up"

 

isn't your croth suppose to be erecting when you have an orgasm?
Link to comment
Share on other sites

Pointers can be used to handle new data on the fly, so you don't have to have a set number of integers, but instead have the program create new ones automatically. So you can have a user enter the number 50 and have the program create fifty new integers automatically.

Link to comment
Share on other sites

I had a hard time understanding pointers to begin with too.

 

Basically, pointers are used so you tell your code 'this is where the data is' rather than 'this is the data'. They help avoid duplication and allow you to use arguments in a function without the code having to create a copy (ie. passing by reference vs. passing by value). This is especially important with arrays since pointers allow you to say where the array is, as opposed to giving it a copy.

 

They're also essential when creating objects dynamically since the code will create the object 'somewhere' in memory, and give you a pointer to tell you where it is.

 

Pointers rock my world.

31805323.png
Link to comment
Share on other sites

Yea well pointers can help a lot but are sometimes a pain in the ass when they just refuse to work, especially in granny-like MFC Applications.

 

Like Barg said, variables with no pointers are basically the value you're looking for. But a variable with a pointer is nothing but "adress" of the value, e.g the bucket where the value is going to be in sooner or later.

 

It's kinda hard to understand and even if you understand it, it's hard to get it done but it's possible.

 

If you have a problem, always try to set any variable to =0; or =1; or so, or else the application crashes if it's the default -22,99994654 * 10 ^ 12 or so tounge.gif

8jjZhSV.jpg

Link to comment
Share on other sites

Yeah, the difficult thing about pointers is that even if you know what they are you've no idea what they are useful for.

 

As someone said, they are essential when allocating memory dynamically, which you can use to have an array whose length is only known at run-time.

 

Also, they were used in C to pass a variable by reference to a function, meaning that function can change directly the contents of that variable rather than using a copy (pass by value).

 

In C++ you can use the reference ('&') operator. Be careful, although the "adress of" and "reference" operators are represented by the same symbol they are different.

 

C/C++ is a good language to get to know pointers well I think. They are used mostly in more complicated projects and not, say, a simple adder.

Link to comment
Share on other sites

The only reason you don't get pointers is because they usually teach C++ backwards. Pointers are everything. Or everything are pointers. Whichever you prefer. Every variable, internally, is treated like a pointer. Every function call is done with pointers. Passing parameters to functions and storing local variables, all of that is done with stack pointers. If you would start learning programming from Assembly, you'd know nothing but pointers by now. But since you did start backwards, here are some important uses.

 

1) Arrays. Every array is a pointer. Calling x[n] is equivalent to calling *(x+n). Try it.

2) Dynamic memory allocation. You often don't know how much memory you will need when the program begins. You can ask for more using malloc or new. Later, C++ only. But how do you know where the memory is allocated? The function that performs allocation returns a pointer.

3) Lists and trees. Yes, you can usually do these with arrays, but you'd have to keep allocating and deallocating memory, and that takes time. Imagine a structure struct list{int d, list *next}; This structure contains an integer d and a pointer to the next element in the list. You can also have several pointers to following elements, and then you have a tree instead of a list. Great many uses to these in graphics, databases, and AI.

4) Functions. Every function is a pointer to a place in memory where code for the function is stored. Consider the following code:

 

void increment(int &x){x++; }int main(void){int x=5;void (*foo)(int &);foo=increment;foo(x);}

 

As you may imagine, foo(x) does the same as increment(x). That is because both are just pointers to where the function is actually located.

5) Passing variables. You don't really see how it happens, because registers are used for it, but it is done with a stack pointer. If you are interested in this, get some text on disassembly.

Prior to filing a bug against any of my code, please consider this response to common concerns.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • 1 User Currently Viewing
    0 members, 0 Anonymous, 1 Guest

×
×
  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.