0
Can somebody explain to me more about pointers as I have seen in c++ please ?
2 Answers
+ 3
Say you own.. a bible. And you want to share a chapter with your friends on WhatsApp.
You could either spend a few hours typing it and send them the whole chapter, or you could say, "Look up Mark 4".
"Mark 4" is like a pointer to text in the bible. "Mark 4" is short to type and everyone will know where to look to get the full text.
In C++, whenever you call a function and pass a parameter, a copy of the parameter will be created.
void send(verse v) {
whatsapp->send(v);
}
verse v = "Again Jesus began...";
send(v); // A copy of `v` is made here.
And copying can be expensive, if the object is very large. It's easier to just pass a pointer, you need to copy less.
void send(verse* v) {
whatsapp->send(*v); // `*v` means, look at the value (bible verse) behind the pointer (mark 4).
}
verse v = "Again Jesus began...";
send(&v); // A pointer to `v` is made here. There is less copying because a pointer is just a number and copying a single number is fast.
+ 1
Explain to me more