Article 37 - C++ Multiple Pointer Asterisks

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.

Multiple pointer representation in a block of memory addresses.

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[10]". We can retrieve any of the ten elements by array index such as array[0] ... array[9]. 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[10][10]", 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.

Pointer usage.

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.

Comments (35)

Posted by anonymous - 20mg levitra at Friday, March 15, 2013 4:03 PM

wow, awesome blog.Thanks Again. Fantastic.

Posted by anonymous - GScraper at Tuesday, March 19, 2013 5:53 AM

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

Posted by anonymous - mac cosmetic outlet at Sunday, March 24, 2013 2:01 PM

http://www.maccosmeticwholesaler.com/Source mac cosmetic outlet http://fakemacmakeup.webs.com/

Posted by anonymous - nytawcozo at Sunday, March 24, 2013 9:55 PM

r0urmK <a href="http://qrtcvhddjnhl.com/">qrtcvhddjnhl</a>, [url=http://jfbhfwzshbqi.com/]jfbhfwzshbqi[/url], [link=http://dfxljpfdoyba.com/]dfxljpfdoyba[/link], http://vcxzeuwaphfd.com/

Posted by anonymous - wow power leveling at Thursday, March 28, 2013 1:43 AM

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

Post a comment

  • Name:
  • Post:
  • Challenge:

Register or login to post comments easier.


Vantasy World Copyright 2011 - 2017. Vantasy World is a project developed by Vantasy Online. Privacy Policy.