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.
wow, awesome blog.Thanks Again. Fantastic.
GScraper it is one new blackhat Search Engine Optimization program, the best scraper and poster for link building, If you have never used it, you real can not imagine that the scrape and POST can crazy to what extent! GScraper http://www.gscraper.com/affiliates/jrox.php?uid=jitesi_1
http://www.maccosmeticwholesaler.com/Source mac cosmetic outlet http://fakemacmakeup.webs.com/
r0urmK <a href="http://qrtcvhddjnhl.com/">qrtcvhddjnhl</a>, [url=http://jfbhfwzshbqi.com/]jfbhfwzshbqi[/url], [link=http://dfxljpfdoyba.com/]dfxljpfdoyba[/link], http://vcxzeuwaphfd.com/
Your means of describing all in this paragraph is truly good, every one be capable of easily understand it, Thanks a lot. wow power leveling http://www.mmogm.com/wow/wow-power-leveling.html