I have only been playing with visual c++ for a couple of weeks now and was just wondering what is the advantage of using pointers. To me that seems to be the hardest thing to understand so far. I am not sure how to use them and what the advantages are to using them.

Dani AI

Generated

For : think of a pointer as a small note that stores the address of a memory cell, not the cell itself. The two basic operators are & (address-of) and * (dereference). Once you can picture "address" vs "value" clearly, most pointer confusion goes away. and pointed to useful reading; below are practical tips and a safe, modern approach to getting comfortable with pointers.

A minimal example to see the mechanics (compile with a modern C++ compiler):

int x = 10;
int* p = &x;   // p holds the address of x
*p = 20;       // follow the address and change the value stored there
p = nullptr;   // mark that p no longer points anywhere valid

Common pitfalls and how to avoid them: never dereference nullptr, avoid using pointers that reference freed memory (dangling pointers), and prevent double-deletes. In modern C++, prefer clear ownership rules: use std::unique_ptr / std::shared_ptr for owning pointers and raw pointers only for non-owning references. Example reference: std::unique_ptr.

Practical steps to learn:

  • Experiment in the debugger: inspect addresses, step through instructions, and watch memory change.
  • Try small, focused exercises: print addresses, transfer a pointer between scopes, then replace raw pointers with references and smart pointers to compare.
  • Use tools: AddressSanitizer, Valgrind or Visual Studio leak detection to catch misuse early.

Note to and others unsure: mastering the mental model and using modern C++ patterns (RAII and smart pointers) removes most real-world need to manage raw pointers directly while still letting you understand how they work.

Recommended Answers

All 5 Replies

Friend, you question is fully-loaded. It is one of those questions that are
very easy to formalate but requires a lot to explain.

I include a couple links that maybe can help you to understand:

one, and the other.

Have fun!

i dont understand still :(

i dont understand still :(

That's OK. There's many things in life that we don't understand. As we gain experience we can fake better and better that we understand. :)

A brief summary of the usefulness of pointers.

1) They can let you use the information in an entire 5kb array (or any other memory hog) in another function having to copy/send only 4 bytes instead of the entire 5kb (or whatever).

2) They allow you to use the (usually) much larger heap memory (aka dynamic memory) as opposed to the (usually) much smaller stack memory. Try declaring an array of 100,000 objects with 100 bytes of memory per object on stack memory and see what happens. HINT--it ain't pretty. Or what if you don't know how many items you're going to need to store when write the program? Do you make your "best guess" and hope your program doesn't crash when you need to use more memory than you allocated or hope you haven't wasted have the memory available to prevent that? Why not have a mechanism, using pointers, to allow you to allocate just as much memory as you need?

3) They allow you to change the value of an item in different function, which is where I usually use them because I don't usually use 5kb arrays or arrays of 100,000 objects uing 100 bytes of memory each.

4) They allow you to store information in a containers that don't require contiguous memory allocation. Things like lists and trees and other goodie things you might learn about later.
And more.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.