Understanding pointers

Asked

Viewed 341 times

4

I’m trying to learn C++ on the Internet, I understand more or less what pointers are but I don’t see a utility, I don’t know anything about C++ I just don’t want to go over this subject, I did a test that I think should work, to see the speed of the pointers vs the normal variables.

Usando pointers Sem usar pointers

Because in both tests the speed is practically the same if one uses pointers and the other does not?

  • 2

    You’re not seeing their usefulness because you’re tracking bad material, so you’re probably learning a lot of things the wrong way. Now I’m running out of time but the answer you’ve gotten so far has not helped much. What you have most is wrong or bad information on the internet. There was a time when that didn’t happen here, but it changed. See help: http://answall.com/tags/c%2b%2b/info More questions that might help: http://answall.com/questions/tagged/ponteiro See this one: http://answall.com/q/56470/101. Pointer is a key concept for languages like C++.

  • 2

    Could [Edit] the question and include the code instead of images? It is too bad to visualize what is written accessing the site by mobile.

  • Thanks for the links. And I don’t need to put the code anymore because @bfavaretto said that array are pointers so the codes are equal, call a function with Pointer.

  • 2

    @mbegginer What the fact of array being pointers has to do with you being able to post the code or not?

  • @Victorstafusa, I think he meant, he’s already figured out what he wanted. But mbegginer what bfavaretto meant, is that when you pass an array to Pointer, you are passing a Pointer to the first element of it... Pointer would point to the first element of the array.

  • Yes, thank you. I thought that so the function would have to copy the entire array, like the "999999 1ns" and then add 1 to the copy and show the result, kkkk but not by the way. And I’m using linuxLive and I don’t have the code anymore it was just a dumb test.

Show 1 more comment

1 answer

1

There is no difference in speed...
Pointers point to a value stored in memory... While variables are predefined spaces in memory for values.

The difference is only in terms of utility. If you have no need, you do not need to use only variable pointers.

  • 1

    Thank you. I saw on the internet that when Voce calls the function it copies the argument, then in the 2 example it should not copy the whole array and the numbers of it, slowing down? Or is it because an array is also a pointer? And also what is the use of pointers then? I searched the internet and saw that you can call a function with some variable and change this variable directly in memory and not change the copy of it. What else to do?

  • Well, like I said, it points to a value stored in memory. It would be something like Assembly Lea... You can use it to modify values directly from memory... Example: You can modify values in the memory of another process, needing only the address in memory.... In Assembly has 1001 utilities, since it is low level... But I don’t see much use in C++ apart from that, rsrs. But anyway, if you need to modify values in memory for some reason, pointers are useful.

  • 3

    @mbegginer Your example was unfortunate, when you pass an array you are actually passing a pointer to the first element of it.

  • Thanks now that I realize this kkkkk, I made 2 equal codes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.