The daily ramblings …

C++ : The Concept of Pointer

Posted in C++ General by akamal on January 16, 2007

I was doing my normal programming routine today, when halted for request on ad hoc pointer tutorial by some of my team members. The 1 hour & 30 minutes tutorial spoke for itself, seeing how hard it is for newcomers to grasp the concept.

Throughout the session, questions bombarded ranging from “why the need to pass between functions” to “how important as to store address of a var”? and so on.

So I’ve come up with this small article on this, just a reminder to those who’s so regularly confused with the concept, I know I do.

[JUSTIFICATION]
When programming for software requiring large design, there’ll be hundreds, literally thousands of codes meddling in functions here and there. Especially on a critical / memory heavy s/w, unnecessary usage of memory is certainly a thing to avoid.

The use of pointer allows programmer to keep on using the same variable, WITH the same memory location, passing it to any external functions so as long it requires.

A safe practise of programing always involves pointers, especially in C++. Since we do not have the luxury of automatic memory management, as Java does, those who codes in C++ must always keep in mind on variable allocation and de-allocation (aka deletion).

[IMPORTANT SYMBOLS]

& is a reference operator. Also means “address of variable to point to”
* means “is a dereference operator. Also means value pointed by the variable”

[EXPLANATION]

To understand what the 2 above means, please have a look at the codes below. No point dwelling on lengthy explanations, right? I’d better kick it off with codes review! Let’s begin..

Ok, in line 1 you have a variable var1 of type, say, int. var1 is then assigned the value of 25. Next, you declare

[EXAMPLE SNIPPET]
int var1 = 25;
int *var2;
var2 = &var1;
cout << var2;
cout << *var2;

1 int var1 = 25;
2

var1 = 25; // var1 hold the value 25 in some memory address, say address 1000
2 var2 = &var1; // var2 stores the address of var1, so when typing …
3 cout << var2; // you would get the address of var1, which is 1000
4 var3 = *var2; // now var3 will take the value pointed by var2
5 // var2 has address 1000, and value pointed by address 1000 is 25

[COMMENT]
The funny thing is a lot of people may understand this concept through books and tutorials, but
when it comes to “real” programming, they’d be perplexed with the way values and address are passed all
over.

I say, that the only way to fully grasps this concept, is to practice regularly with pointers, yeah.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: