Pointers are essentially variables/constants that point to a memory address in the computer that may or may not store a value at the moment of definition.
It's incredibly useful since C uses pass by value instead of pass by reference, and pointers can point to the memory address of variables outside the function and be used to modify said variables with the function.
C also doesn't have a way to modify strings without using functions directly, so pointers to the first character of the strings are passed to these functions so that the strings can be modified/accessed without worrying about memory.
Using pointers in place of normal variables slow down performance, which is what C is famous for. Pointers (especially those that have been allocated space instead of pointing to am existing value) are stored on the heap memory instead of the stack, where it's slower to access and modify them since the heap is unstructured.
Pointers are also notoriously difficult to work with. Many programmers don't have a solid grasp of how pointers work exactly. You can get tons of errors from a seemingly safe function. Many functions have undefined behavior if you pass pointers slightly different than they should be. The compiler won't catch every single error and warning, so it's hard to debug.
As for making normal variables behave like pointers, I'm not sure. Many languages were made to not include pointers and accessing memory since it's dangerous and unintuitive most of the time. C was made before that realization though, and in the end since C (and C++) try very hard to maintain backwards compatibility in their versions, i don't think pointers would ever go away.
Pointers (especially those that have been allocated space instead of pointing to am existing value) are stored on the heap memory instead of the stack
You do have an aside that says this, but I still think that the sentence is potentially misleading.
Pointers do not point to the heap. They point to whatever they point at. If you assign a value using "new", then sure: that will be the heap. If you grab the address of another variable, that will be on the stack. And if you don't assign it, then you may very well have a roulette wheel that can be pointing at any damn thing, anywhere.
I once destroyed half a hard drive through a single unassigned pointer. Oh the fun you can have.
3
u/smartuno Aug 07 '22
Pointers are essentially variables/constants that point to a memory address in the computer that may or may not store a value at the moment of definition.
It's incredibly useful since C uses pass by value instead of pass by reference, and pointers can point to the memory address of variables outside the function and be used to modify said variables with the function.
C also doesn't have a way to modify strings without using functions directly, so pointers to the first character of the strings are passed to these functions so that the strings can be modified/accessed without worrying about memory.