DavidS,
I must admit I did not know that poitners really are difficult for some people to understand.
Pointers are not difficult for anyone to understand.
Using them correctly in non-trivial programs turns to be almost impossible. They have been the source of millions of bugs in all kind of code since forever. Especially where large teams are involved and code passes though many hands in it's life span. And that is in code written my skilled, experienced, intelligent programmers.
For this reason so much effort has been put into the "sanitizer" tools you now find in Clang/LLVM and GCC.
Just have a look though all the security vulnerabilities reported on all kind of projects to understand what I'm talking about.
And then you go on to provide a very limited case example.
What is wrong with that. It a short an sweet example to demonstrate the point. As examples should be. The idea there is easily expandable to whatever size of program and complexity you like.
If you want to be to the point, then show accessing many different data structures in one area of code from a single pointer (as is very easy in BBC BASIC, and doable in C), without using any other variables.
You mean like this:
Code: Select all
let data0 = {
x: 50,
y: 60
}
let data1 = {
t: "Hello",
u: "world"
}
let node2 = {
a: [1, 2, 3, 4],
b: 99.2
}
myPointer = data0
console.log(myPointer.x)
pointer = data1
console.log(myPointer.u)
pointer = data2
console.log(myPointer.a)
Of course what you want to do there is very bad programming practice. Using the same name to refer to very different types of things.
In C/C++ that sort of thing requires type casts which brings us to that world of bugs cause by mistakes using pointers. When you type cast pointer you are disabling the compilers ability of warning you of buggy situations. These bugs can be hard to spot and hard to debug.
Memory in C++ is a leaky abstraction .