+ 2

Why do I need to know about pointers? What is its need?

Actually I'm confused about the use of pointers. Do I really need to use pointers??

1st Dec 2016, 9:53 AM
Nazmul Islam
Nazmul Islam - avatar
4 Réponses
+ 2
This is an example of pointer: public void swap(int *a, int *b) { a^=b; b^=a; a^=b; } int a=10,b=8,c=12,d=13; swap(&a, &b); swap(&c, &d); You can see. If you don't use pointer you can't swap the value of a and b, c and d by use swap function.
1st Dec 2016, 9:58 AM
Nguyễn Hoàng Long
Nguyễn Hoàng Long - avatar
+ 1
So, only if I need to use the swap them, I have to use pointers, right??
1st Dec 2016, 10:13 AM
Nazmul Islam
Nazmul Islam - avatar
+ 1
Pointers are typically used to pass large chunks of data around. They're basically arrays and can be accessed in the same way. For example, if you get into game programming, you may end up using a graphics library such as Direct3D or OpenGL, within which you would need to set up textures for your models. You can't just tell them to open files and read their contents, since that's not what they're designed for. You have to find some other way to open those files and copy them into memory, but once that's done, ya just pass the pointer of the image in memory to the library and it sends it off to the graphics card.
1st Dec 2016, 4:08 PM
Nemo
0
If you're using C++, just learn it. If you can't stand it try something like Python. What's important in that code example is that the variables are passed by reference and not by value. It doesn't look like much but if you are trying to send a 50000 character string you want a pointer to that string and not the string itself. You can probably avoid it for a little while but you're learning a language that expects you to understand what a language like Python does for you.
1st Dec 2016, 11:34 AM
Kirk Schafer
Kirk Schafer - avatar