Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C++, are pointers like cell references in an Excel Worksheet?

I'm an average programmer in C++, and usually avoid pointers at all costs. But I have recently started using some APIs, that are just full of them. And honesty, they seem to work a lot better, if I just continue down a similar path, as that intended/implied by the author of the API.

In form and function, but obviously not syntax - Can "Pointers" be looked at as being, "even somewhat", analogous to "Cell References" in Microsoft Excel?

I have several years of experience with Microsoft Excel w/VB. I'm also sort of a "Formula/Reference Junkie", when it comes to using Excel, so this train of thought just makes sense to me. I just want to know if I'm "Pointed" in the right direction.

Thank You!

like image 649
tahwos Avatar asked Jan 24 '12 03:01

tahwos


2 Answers

A cell reference in Excel is a string that indicates a certain location in a workbook (or, under certain circumstances, another workbook).

A pointer in C++ is a memory address. The memory address is a number (nothing more!) that locates a certain spot in virtual RAM.

So yes, they actually are rather similar. I was prepared to rain on your parade by talking about how different the two are. But the analogy works.

When you "dereference" a pointer, you're going to the spot in memory which it indicates. In Excel you don't really "dereference" the cell references. But close enough.

When you say int y = 4; int* x = &y;, that's kind of like creating a cell at B1 containing "4", and then another cell that contains "=B1".

like image 111
Borealid Avatar answered Oct 21 '22 13:10

Borealid


There are several analogies that spring to mind, but, the classic one when I first learnt computer programming was mail boxes in the mail room. Imagine mail boxes labelled, Tom, Dick and Harry.

  • In Tom's mail box, place an envelope that says there's a clue in Dick's mail box
  • In Dick's mail box, place an envelope that says the treasure is in Harry's mail box
  • In Harry's mail box, you put $100 dollars! Yippee!

In computer programming, Tom, Dick and Harry are all memory addresses. The first two contain pointers, the third, contains a numerical value. Opening the envelope in Tom or Dick's mailboxes are like dereferencing pointers.

We write this:

int harry = 100;
int *dick = &harry;
int **tom = &dick;

So, **tom == *dick == harry == 100. Whilst tom is a pointer to a pointer to harry. And dick is a pointer to harry.

like image 28
Stephen Quan Avatar answered Oct 21 '22 14:10

Stephen Quan