My biggest problem right now in understanding c++ is pointers.

Im curious to its specific funcionality, so instead of doing somthing like
p = &x
then calling x using *p, why couldn't you just say x = p?

Im pretty sure my syntax is wrong, but hopefully i'm getting my question across properly. Thanks!

Dani AI

Generated

Short answer: you cannot just do "x = p" because those two names represent different things. One is a storage location holding a value (x). The other is a variable that stores a memory address (the pointer). Assigning an address into a value is a type-and-semantics mismatch, so the compiler won’t allow it unless you force a conversion — and forcing it is almost always wrong.

Building on 's apple analogy: think of x as the apple and the pointer as the street address where the apple sits. Saying "give me the apple" is different from "give me the address of the apple." To get the apple when you have the address you must follow that address and take the apple; the language provides operators to take the address of an object and to access (dereference) the object at an address.

A few practical clarifications missing above:

  • Types matter: a pointer-to-T and a T are distinct types. The compiler enforces that.
  • Pointers can be null or can point at freed memory; dereferencing an invalid pointer gives undefined behavior. Initialize pointers and prefer nullptr instead of old NULL/0 in modern C++.
  • References are aliases (cannot be reseated and normally cannot be null); pointers can be reassigned and can represent "no object." As noted, use the right tool for the job.
  • In modern C++ prefer RAII and smart pointers (std::unique_ptr, std::shared_ptr) to manage ownership instead of raw owning pointers.

Quick practice steps: inspect addresses and values with a simple program and a debugger, compare passing by value vs by pointer vs by reference to functions, and deliberately try invalid cases (null or dangling) to see how the runtime/compiler responds. The tutorial pointed to is a useful deep dive; after that, read a modern C++ resource on smart pointers and ownership to round out safe usage.

Recommended Answers

All 4 Replies

pointers are just like ordinary variables that carry the address of other variables and objects. you need pointers because sometimes when passing information within your program, let say from a function to a function, you dont want to pass all the data, instead you want to pass the address of that data in memory, that is when pointers become useful. they are also used for memory management as they are able to address to memory.

i have an apple analogy for you, if you ask for an apple, i can bring it to you, or i can say that it is in the kitchen and you can go and grab it. i am not a good c++ programmer though, but it is easy to understand things when some one is as bad as you :)

I also had a big problem understanding pointers, but it's not that hard:
pointers are just like any other variables when you use the asterisk (*) before them - which leads to the value in the address they hold.
They're useful because, just how serkansendur said, you can save computer memory when passing them to functions (because you send the whole address and not a copy of the variable), and you can manage the variables' values throught them.
Passing an address to a pointer in a function is similar to sending a reference (with the prefix &) to a function, except that a reference can't get a new address to refer to.

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.