When are pointers used and why

I dont get it why i should use pointers in my projects. Is it just me or me not that advanced to use them.

From the lesson on pointers:
image

The purpose of the lesson seems to be, so you’ll recognize what a pointer is and how it works if you see it in older code. There really isn’t a reason to use pointers anymore.

That doesn’t sound right to me.
References can’t be dereferenced, they are much less powerful

May as well use java/equivalent at that point, right?

1 Like

Perhaps it’d be more fair to say that STL deals with most common application logic where one might otherwise use pointers?

As an example, vectors are much easier to deal with than arrays because with arrays one has to deal with making sure it’s on the heap so that it can be passed around with copying, plus having to malloc and free it and then if for some reason one wants to change the shape of it then hahaha have fun with that.

1 Like

I agree that my previous comment was a bit hasty. From some very recent research spurred by your comment, I can see where an experienced programmer might find pointers very useful. I’m not sure that dereferencing is one of them. As I understand it, dereferencing is simply accessing the value stored at the 'pointed to ’ memory location. Could you expound what the difference you’ve referred to is?

My wording is a bit backwards. Pointers are references as values, so you could for example keep a list of references (perhaps a pointer to where there are more pointers - an array), and you can do arithmetic on them too if you know the layout of the memory in that location.
Cpp references really only solve one problem: avoiding making a copy when making a function call.

1 Like

A quick (and very rudimentary) example:

int main() {
  int nums[] = {100, 200, 300};
  int *np, ni;
  np = nums;

  for (ni = 0; ni < 3; ni++)
  {
    std::cout << *np << "\n";
    np++;
  }
}

Output to stdout:

100
200
300
1 Like