+ 6
Is anyone else having a hard time with pointers?
I don't like pointers, I can't seem to wrap my mind around the "point" of them. =D But seriously I have a hard time any time they show up. Any advice would be awesome. Thanks
7 Respuestas
+ 21
In C/C++ even Swift and C# the pointers are the fundamental principles of programming. Without the knowledge of how to use them or their meaning you are stuck in a dead end. So pointer, what is it? It's a cell in the memory, not even a special one, that contains some random value like all other cells. But when you give that cell the memory address of another cell, usually with "&", now that is called a pointer. Why it's called pointer, because it points to another part of the memory. The address is just a number to differentiate one cell from another. In different languages accessing the value of the pointed cell is handled differently so I'm not going to discuss that. Overall you must understand pointers, they are not so complex as you think.
+ 5
Three reasons that I can think of using pointers instead of calling the variable itself:
1.- The performance of your program will be faster in many situations.
2.- They are needed to fully have access and use the heap (dynamic memory) in C/C++.
3.- Pointers are needed to call a function by reference in C/C++.
So, yeah.. ponters could be tricky to learn at first but you have to if you want to use the full power and features of C/C++.
+ 1
I need to join. Can anyone give a specific example, when calling a variable instead a pointer is not possible or would give a false result?
+ 1
Pointers as memory locations are useless and dangerous. They are a reminiscent of the past (assembly, C). Don't confuse them with references, which is a useful notion, for example in call by reference. C/C++ implement call by reference using the ugly */& operators. C# opted the much simpler, intuitive ref designator.
+ 1
pointer is not reference. C# does not have memory location pointers. memory is managed by the heap
0
OK so I think I may have just gone over that section too fast. But I still didn't understand why you would use a pointer to call a variable instead of just calling the variable its self. Does that sound right?
- 1
kk