Have you ever had trouble with pointers with programming in C/C++? Do you come from Java or any other programming language that does not use memory address indirect dereferencing? Do not worry, this article will demystify all the pointer asterisks troubles you will ever have.
Pointers are great but they can be a lot of trouble to use because they are forms of indirection to the object in question. I will refer to object as a basic entity of the programming language, for example, objects in C++. In the rest of this articles, I will discuss about pointers in the context of C/C++.
To understand pointer usage, such as applying one or more dereferences using asterisks, we need to be clear what a pointer is. A pointer is simply an object. It is extremely important to understand that a pointer has a size of memory address on a given architecture (some architecture may include additional overhead, although unlikely). Thus, when you use a C command like "sizeof( pointer )" it should give you the size of a memory address location. For example, on a 32 bit architecture, it would return 32 bits in terms of size, or a word. Notice that there was no asterisk in this example because we referring to the pointer object itself and not to what the pointer points at.
On the other hand, if you used "sizeof( *pointer )", it will return the sizeof the object that the pointer points to. In this case, if the pointer was pointing to a 32 bit integer, then it would return 32 bits. If the pointer was pointing to a 8 bit char, it would return 8 bits. Make sure you are clear with the basics of a pointer.
The asterisks means to dereference. Actually, ignore that term, it is confusing. Instead, think of asterisk as "retrieving the object that is being pointed by the pointer". So if you had multiple asterisks, then it becomes "retriving the object that is being pointed by the object that is being pointed by the pointer" and so forth. It is a recursive definition.
Typically, multiple asterisks is often used in array objects. Suppose we have an array of ten integers, "int array". We can retrieve any of the ten elements by array index such as array ... array. With pointers, we can do something similar. If we assign a pointer that pointers to the array like "int * pointer = array", then we can get the first value by using dereferencing "*pointer".
If we had a two dimensional array, such as "int array", we can can use two asterisks to find the first row's first col's element by "**pointer". Did you notice that the number of asterisks matches the number of paired square brackets in the array?
In fact, an array in C/C++ is just a memory reference. So, a paired square bracket is just another form of dereferencing, like using the asterisk. That's why in the pointer declaration and initalization (int * pointer = array), we did not use the (memory address) referencing ampersand (&) token.
Often, when you assign a different object that you want a pointer to point to, you just change the value of the pointer. If you are having problems grasping this concept, just think of a pointer as an int (somewhat). So, when you dereference a pointer (*pointer), you are just asking the program to retrieve the object at the memory address stored in this pointer, most often in an integer memory address.
One misconception in changing what the pointer points to inside a function is to simply pass in a pointer. This is incorrect. For example, the following function signature does not work as intended, "void swapPointers1( void * pointer1, void * pointer2 )". Instead, we need "void swapPointers2( void ** pointer1, void ** pointer2 )". We need two asterisks because what we pass in is only a copy of a pointer, so in the first signature, the function would only change the copied pointer object. But in the second signature, the function can change the pointer object because the copied pointer points to the pointer we want to change, namely the double asterisks.
Remember, you need one extra asterisk if you want to change what the pointer points to inside a function.
Hopefully, this article has clarified the mysteries of pointer dereferences.
Keep up the fantastic work , I read few content on this web site and I believe that your site is really interesting and has got bands of fantastic info .
<a href="http://www.outletmulberryuk.co.uk" title="mulberry outlet">mulberry outlet</a>
Great ?I should definitely pronounce, impressed with your web site. I had no trouble navigating through all the tabs and related information ended up being truly simple to do to access. I recently found what I hoped for before you know it in the least. Reasonably unusual. Is likely to appreciate it for those who add forums or something, site theme . a tones way for your client to communicate. Nice task.
[url=http://www.superclassicboots.com/ugg-1873-bailey-button-c-1.html]ugg bailey button[/url]
<a href="http://www.superclassicboots.com/ugg-1873-bailey-button-c-1.html" title="ugg bailey button">ugg bailey button</a>
Very interesting topic , thankyou for posting . "He who seizes the right moment is the right man." by Johann Wolfgang von Goethe.
[url=http://howtolosestomachfatforwomen.blogspot.com]How To Lose Stomach Fat[/url]
<a href="http://howtolosestomachfatforwomen.blogspot.com" title="How To Lose Stomach Fat">How To Lose Stomach Fat</a>
Hi there very nice site!! Man .. Beautiful .. Amazing .. I'll bookmark your site and take the feeds additionally?I am|I'm} satisfied to seek out a lot of helpful information right here within the submit, we want develop extra techniques in this regard, thanks for sharing.
<a href="http://pinterest.com/daniel0128/mulberry-bags/" title="mulberry bags">mulberry bags</a>
I've been surfing online greater than 3 hours today, yet I by no means discovered any attention-grabbing article like yours. It's beautiful worth sufficient for me. In my opinion, if all website owners and bloggers made just right content material as you probably did, the web can be a lot more useful than ever before. "I thank God for my handicaps, for through them, I have found myself, my work and my God." by Hellen Keller.
<a href="http://www.cameraamazon.info/" title="www.cameraamazon.info">www.cameraamazon.info</a>