Social Widget

Responsive Ads Here

Thursday, January 2, 2020

What is the difference between *p,**p, ***p in pointers?

Well, before spotting out the differences between *p, **p and ***p in pointers, I will first share what pointer is…

So, what is a pointer?

A pointer is a variable that contains the address in memory of another variable. We can have a pointer to any variable type. The unary or nomadic operator & gives the ``address of a variable''. The indirection or dereference operator * gives the ``contents of an object pointed to by a pointer''.

To declare a pointer to a variable do:

 int *p;

Now, *p vs **p vs ***p????

*p: *p is a pointer to a variable, as shown below. It is also called a single pointer. The single pointer has two purposes: to create an array and to allow a function to change its contents (pass by reference).

**p: **p is a pointer to a pointer variable, also called a double-pointer. It is a form of multiple indirections or a chain of pointers. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value as shown below. The double-pointer can be used in a 2D array (or array of arrays, since each "column" or "row" need not be the same length).

***p: ***p is a pointer to a double pointer, rather a pointer to a pointer to a pointer variable, as shown below. It is mostly called a triple pointer. It is an even higher level of multiple indirections or pointer chaining. A triple pointer is used to traverse an array of pointers.

Implementing *p, **p and ***p…


#c #c++ #pointers

No comments:

Post a Comment