<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Praphul Kolte</title>
    <description>The latest articles on DEV Community by Praphul Kolte (@pskolte84).</description>
    <link>https://dev.to/pskolte84</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1064549%2Fa4eff5c3-ab01-42fd-9cae-12dd01f2b2ef.jpg</url>
      <title>DEV Community: Praphul Kolte</title>
      <link>https://dev.to/pskolte84</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pskolte84"/>
    <language>en</language>
    <item>
      <title>Ways to filter list in java</title>
      <dc:creator>Praphul Kolte</dc:creator>
      <pubDate>Sun, 23 Apr 2023 17:32:08 +0000</pubDate>
      <link>https://dev.to/pskolte84/ways-to-filter-list-in-java-4ohf</link>
      <guid>https://dev.to/pskolte84/ways-to-filter-list-in-java-4ohf</guid>
      <description>&lt;p&gt;List is commonly used Collection in Java. Many times List needs to be filtered by some attribute or property. Example: Filter the Employee list to get list of permanent employees only. There are many ways to filter list. This blog focus on filtering list with different criteria.&lt;/p&gt;

&lt;p&gt;For filtering example we are considering list of Apples. List has mix of apples with red and green color with different weight and taste.&lt;/p&gt;

&lt;p&gt;Lets create Apple class with color, weight and  taste with getter and setters implemented.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package com.filter.fruits;

public class Apple {
    private int weight;
    private String color;
    private String taste;


    public Apple() {
        super();
    }

    public Apple(int weight, String color, String taste) {
        super();
        this.weight = weight;
        this.color = color;
        this.taste = taste;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getTaste() {
        return taste;
    }

    public void setTaste(String taste) {
        this.taste = taste;
    }

    @Override
    public String toString() {
        return "Apple [weight=" + weight + ", color=" + color + ", taste=" + taste + "]";
    }

}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets create a utils class to get List of mixed  Apples.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package com.filter.fruits;

import java.util.ArrayList;
import java.util.List;
//This class create methods for getting pre-filled list of apples
public class AppleUtils {
    public static List&amp;lt;Apple&amp;gt; getAppleList() {
        List&amp;lt;Apple&amp;gt; applesList = new ArrayList&amp;lt;&amp;gt;();
        applesList.add(new Apple(100, "Green", "Sour"));
        applesList.add(new Apple(200, "Red", "Sweet"));
        applesList.add(new Apple(150, "Green", "Sour"));
        applesList.add(new Apple(250, "Red", "Sweet"));
        applesList.add(new Apple(150, "Green", "Sour"));
        applesList.add(new Apple(150, "Red", "Sweet"));
        applesList.add(new Apple(200, "Green", "Sour"));
        return applesList;
    }

}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we need to create methods to filter out apples.&lt;br&gt;
AppleFilterUtils  has methods as below.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;filterByRedColor&lt;/em&gt; methods filters by "Red" color which is not flexible&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;filterByGreenColor&lt;/em&gt; methods filters by "Gree" color which is not flexible&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;filterByColor&lt;/em&gt; methods filters by color which is passed from outside. This method can be used for green as red apple filtering&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;filterByColorAndWeight&lt;/em&gt; methods filters by color and weight by passing them from outside&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;filterByCritera&lt;/em&gt; methods filters criteria passed using Predicate class. Predicate class define criteria.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package com.filter.fruits;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

public class AppleFilterUtils {
    public static List&amp;lt;Apple&amp;gt; filterByRedColor(List&amp;lt;Apple&amp;gt; appleList){
        List&amp;lt;Apple&amp;gt; filteredList = new ArrayList&amp;lt;&amp;gt;();
        for(Apple apple : appleList) {
            if("Red".equals(apple.getColor())) {
                filteredList.add(apple);
            }
        }
        return filteredList;
    }

    public static List&amp;lt;Apple&amp;gt; filterByGreenColor(List&amp;lt;Apple&amp;gt; appleList){
        List&amp;lt;Apple&amp;gt; filteredList = new ArrayList&amp;lt;&amp;gt;();
        for(Apple apple : appleList) {
            if("Green".equals(apple.getColor())) {
                filteredList.add(apple);
            }
        }
        return filteredList;
    }

    public static List&amp;lt;Apple&amp;gt; filterByColor(List&amp;lt;Apple&amp;gt; appleList, String color){
        List&amp;lt;Apple&amp;gt; filteredList = new ArrayList&amp;lt;&amp;gt;();
        for(Apple apple : appleList) {
            if(color.equals(apple.getColor())) {
                filteredList.add(apple);
            }
        }
        return filteredList;
    }

    public static List&amp;lt;Apple&amp;gt; filterByColorAndWeight(List&amp;lt;Apple&amp;gt; appleList, String color, int weight){
        List&amp;lt;Apple&amp;gt; filteredList = new ArrayList&amp;lt;&amp;gt;();
        for(Apple apple : appleList) {
            if(color.equals(apple.getColor()) &amp;amp;&amp;amp; weight &amp;gt;= apple.getWeight()) {
                filteredList.add(apple);
            }
        }
        return filteredList;
    }


    public static List&amp;lt;Apple&amp;gt; filterByCritera(List&amp;lt;Apple&amp;gt; appleList, Predicate&amp;lt;Apple&amp;gt; predicate){
        List&amp;lt;Apple&amp;gt; filteredList = new ArrayList&amp;lt;&amp;gt;();
        for(Apple apple : appleList) {
            if(predicate.test(apple)) {
                filteredList.add(apple);
            }
        }
        return filteredList;
    }

}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Custom Predicates can be created by inheriting Predicate class.&lt;/p&gt;

&lt;p&gt;Lets create Predicate for Green apples and another for filtering apples having weight more than 150g&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package com.filter.fruits.predicates;

import java.util.function.Predicate;

import com.acts.fruits.Apple;

public class AppleColorPredicate implements Predicate&amp;lt;Apple&amp;gt;{


    @Override
    public boolean test(Apple t) {
        return "Green".equals(t.getColor());
    }

}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package com.filter.fruits.predicates;

import java.util.function.Predicate;

import com.acts.fruits.Apple;

public class AppleWeightPredicate implements Predicate&amp;lt;Apple&amp;gt;{

    @Override
    public boolean test(Apple t) {
        return t.getWeight() &amp;gt; 150;
    }

}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets take look at below Tester class to test Filter methods from AppleFilterUtils class&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package com.acts.fruits.tester;

import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import com.filter.fruits.Apple;
import com.filter.fruits.AppleFilterUtils;
import com.filter.fruits.AppleUtils;
import com.filter.fruits.predicates.AppleColorPredicate;
import com.filter.fruits.predicates.AppleWeightPredicate;

public class AppleFilterTester {

    public static void main(String[] args) {
        //Getting apple list from utils class
        List&amp;lt;Apple&amp;gt; applesList = AppleUtils.getAppleList();
        System.out.println("1.All Apples\n" + applesList);

        //Filter By Red color
        List&amp;lt;Apple&amp;gt; filteredList1 = AppleFilterUtils.filterByRedColor(applesList);
        System.out.println("2.Red apples\n" + filteredList1);

        //Filter By Green color
        List&amp;lt;Apple&amp;gt; filteredList2 = AppleFilterUtils.filterByGreenColor(applesList);
        System.out.println("3.Green apples\n" + filteredList2);

        //Passing color as parameter
        //Filter By color: Red
        List&amp;lt;Apple&amp;gt; filteredList3 = AppleFilterUtils.filterByColor(applesList, "Red");
        System.out.println("4.Red apples\n" + filteredList3);

        //Filter By color: Green
        List&amp;lt;Apple&amp;gt; filteredList4 = AppleFilterUtils.filterByColor(applesList, "Green");
        System.out.println("5.Green apples\n" + filteredList4);

        //Filter By color: Green and weight &amp;gt;150
        List&amp;lt;Apple&amp;gt; filteredList5 = AppleFilterUtils.filterByColorAndWeight(applesList, "Green", 150);
        System.out.println("6.Green apples\n" + filteredList5);

        // Using predicate from java8
        Predicate&amp;lt;Apple&amp;gt; appleColorPredicate =  new AppleColorPredicate();
        //Filter By color: Green  using predicate
        List&amp;lt;Apple&amp;gt; filteredList6 = AppleFilterUtils.filterByCritera(applesList, appleColorPredicate);
        System.out.println("7.Green apples\n" + filteredList6);

        // Using predicate from java8
        Predicate&amp;lt;Apple&amp;gt; appleWeightPredicate =  new AppleWeightPredicate();
        //Filter By weight &amp;gt; =150  using predicate
        List&amp;lt;Apple&amp;gt; filteredList7 = AppleFilterUtils.filterByCritera(applesList, appleWeightPredicate);
        System.out.println("8.With Weight apples\n" + filteredList7);

        // Using predicate from java8
        Predicate&amp;lt;Apple&amp;gt; predicate = (apple) -&amp;gt;  apple.getColor().equals("Red") &amp;amp;&amp;amp; apple.getWeight() &amp;gt; 200;
        List&amp;lt;Apple&amp;gt; filteredList8 = AppleFilterUtils.filterByCritera(applesList, predicate);
        System.out.println("9.Red With Weight apples\n" + filteredList8);

        // Using predicate and stream api from java8
        List&amp;lt;Apple&amp;gt; filteredList9 = applesList.stream()
        .filter(predicate)
        .collect(Collectors.toList());
        System.out.println("10.Red With Weight apples\n" + filteredList9);


        // Using Stream and predicate on the fly and stream api from java8
        List&amp;lt;Apple&amp;gt; filteredList10 = applesList.stream()
                .filter((apple) -&amp;gt;  apple.getColor().equals("Green") &amp;amp;&amp;amp; apple.getWeight() &amp;gt; 150)
                .collect(Collectors.toList());
        System.out.println("11.Freen With Weight apples\n" + filteredList10);

        // Using Stream and predicate on the fly and stream api from java8
        List&amp;lt;Apple&amp;gt; filteredList11 = applesList.stream()
                .filter((apple) -&amp;gt;  apple.getColor().equals("Green"))
                .filter((apple) -&amp;gt;  apple.getTaste().equals("Sweet"))
                .collect(Collectors.toList());
        System.out.println("12.Green With Taste apples\n" + filteredList11);


    }

}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>java</category>
      <category>collections</category>
      <category>filter</category>
      <category>list</category>
    </item>
    <item>
      <title>Allocating dynamic memory to 3D array</title>
      <dc:creator>Praphul Kolte</dc:creator>
      <pubDate>Sat, 22 Apr 2023 17:50:48 +0000</pubDate>
      <link>https://dev.to/pskolte84/allocating-dynamic-memory-to-3d-array-23o7</link>
      <guid>https://dev.to/pskolte84/allocating-dynamic-memory-to-3d-array-23o7</guid>
      <description>&lt;p&gt;Arrays are commonly used containers to store data for processing. Array stores homogeneous elements in contiguous memory. Array indexes start from zero and we can navigate to any elements by using indexes. Base on structure of array there are 2 type&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Single or One Dimensional Array : only one dimension  &lt;/p&gt;

&lt;p&gt;1D Declaration in C++ :  &lt;code&gt;int arr[5];&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ficvon6igkis8rkgduzc2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ficvon6igkis8rkgduzc2.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Multi-dimensional Array : More than one dimension&lt;/p&gt;

&lt;p&gt;2D Declaration in C++ :  &lt;code&gt;int arr[3][5];&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi6f7mdbzerbtzfvk0jto.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi6f7mdbzerbtzfvk0jto.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;3D Declaration in C++ :  &lt;code&gt;int arr[3][3][5];&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpyp5u7twnrpya2fpc8fj.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpyp5u7twnrpya2fpc8fj.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;1D array is collection of elements&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;2D array is collection of 1D arrays&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;3D array is collection of 2D arrays&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;similarly we can say nD array is collection of (n-1)D arrays&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As per language rules array sizes has to be constants which lead to shortage or excess of memory. Developer can allocate memory dynamically at runtime to avoid memory wastage.&lt;/p&gt;

&lt;p&gt;Lets see how to allocate memory for 3D. Why 3D because 3D memory allocation has memory allocation for 1D, 2D and 3D arrays also as 3D arrays is collection of 2D and 2D is collection of 1D.&lt;/p&gt;

&lt;p&gt;Lets start with declaring pointer for 3D array. Pointer to represent 3D array &lt;code&gt;int ***p;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Lets create separate C++ function to allocate memory by supplying dimensions. Here n, r and c are dimensions.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;allocateMemory&lt;/em&gt; function first allocates memory to n number of 2D arrays the allocate for r number of 1D array and then allocate memory for elements Or 1D&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

int *** allocateMemory(int n, int r, int c)
{
    int ***p = new int**[n]; // for n numbers of 2D arrays

    for(int i=0;i&amp;lt;n;i++)
    {
        p[i] = new int*[r]; // for r numbers of 1D arrays
    }

    for(int i=0;i&amp;lt;n;i++)
    {
        for(int j=0; j&amp;lt; r;j++)
        {
            p[i][j] = new int[c]; // for c numbers of elements
        }
    }
    return p;
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Lets create separate C++ function to accept array elements from user. This function receives pointer to memory which already allocated to array.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

void acceptInput(int ***p, int n, int r, int c )
{
    for(int i =0;i&amp;lt;n;i++)
    {
        for(int j =0;j&amp;lt;r;j++)
        {
            for(int k =0;k&amp;lt; c;k++)
            {
                cin&amp;gt;&amp;gt;p[i][j][k];
            }
        }
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Lets create separate C++ function to Print Array elements on console.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

void printArray(int ***p, int n, int r, int c )
{
    for(int i =0;i&amp;lt;n;i++)
    {
        for(int j =0;j&amp;lt;r;j++)
        {
            for(int k =0;k&amp;lt;c;k++)
             {
                cout&amp;lt;&amp;lt;p[i][j][k]&amp;lt;&amp;lt;" ";
             }
        }
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Lets create separate C++ function to deallocate memory by supplying dimensions and pointer of allocated memory.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

void deallocateMemory(int ***p, int n, int r, int c)
{
    for(int i=0;i&amp;lt;n;i++)
    {
        for(int j=0; j&amp;lt; r;j++)
        {
        delete[] p[i][j];
        }
    }

    for(int i=0;i&amp;lt;n;i++)
    {
    delete[] p[i];
    }

    delete[] p;
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now we have created functions to allocate memory , fill data in memory, read memory and finally delete the allocated memory. &lt;br&gt;
Its time to write main function and try calling functions&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

#include &amp;lt;iostream&amp;gt;
using namespace std;

int main()
{
    int n,r,c;
    int ***p = NULL;
    //Read user input
    cout&amp;lt;&amp;lt;"\n Enter first dimension:"&amp;lt;&amp;lt;endl;
    cin&amp;gt;&amp;gt;n;
    cout&amp;lt;&amp;lt;"\n Enter second dimension:"&amp;lt;&amp;lt;endl;
    cin&amp;gt;&amp;gt;r;
    cout&amp;lt;&amp;lt;"\n Enter third dimension:"&amp;lt;&amp;lt;endl;
    cin&amp;gt;&amp;gt;c;

    //Allocating memory
    p = allocateMemory(n, r, c);

    //Read array elements
    cout&amp;lt;&amp;lt;"\n Enter " &amp;lt;&amp;lt; n*r*c &amp;lt;&amp;lt; " elements :"&amp;lt;&amp;lt;endl;
    acceptInput(p,n, r, c);

    //Print array elements
    cout&amp;lt;&amp;lt;"\n Array elements are :";
    printArray(p,n, r, c);

    //Deleting/free up memory
    deallocateMemory(p,n, r, c);

    return 0;
}




&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Sample run output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Enter first dimension:&lt;br&gt;
2&lt;/p&gt;

&lt;p&gt;Enter second dimension:&lt;br&gt;
2&lt;/p&gt;

&lt;p&gt;Enter third dimension:&lt;br&gt;
2&lt;/p&gt;

&lt;p&gt;Enter 8 elements :&lt;br&gt;
11&lt;br&gt;
22&lt;br&gt;
33&lt;br&gt;
44&lt;br&gt;
55&lt;br&gt;
66&lt;br&gt;
77&lt;br&gt;
88&lt;/p&gt;

&lt;p&gt;Array elements are :&lt;br&gt;
11 22 33 44 55 66 77 88 &lt;/p&gt;

</description>
      <category>cpp</category>
      <category>memoryallocation</category>
      <category>3darray</category>
    </item>
    <item>
      <title>Pointers and Constants</title>
      <dc:creator>Praphul Kolte</dc:creator>
      <pubDate>Fri, 21 Apr 2023 17:03:10 +0000</pubDate>
      <link>https://dev.to/pskolte84/pointers-and-constants-40fg</link>
      <guid>https://dev.to/pskolte84/pointers-and-constants-40fg</guid>
      <description>&lt;p&gt;Pointer stores address of variable and it can be referenced and dereferenced. Pointers are powerful which enable developers to play at memory level operations. Sometimes developers may make accidental modifications to values pointed by pointers. To impose restriction on pointers &lt;strong&gt;const&lt;/strong&gt; keyword can be used. Based on usage of &lt;em&gt;const&lt;/em&gt;, there are 3 possible scenarios as below&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pointer to Constant&lt;/strong&gt; : Value pointed by pointer to constant can not be changed but pointer can changed. This type of pointers are used to restrict accidental modifications to value. It provide read only access to value to functions when passed as argument.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Pointer to constant can be declared by placing const keyword before data type.&lt;br&gt;
Example. &lt;strong&gt;const&lt;/strong&gt; int *p = NULL;&lt;/p&gt;

&lt;p&gt;Below code snippet demonstrates restriction on value change.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include&amp;lt;iostream&amp;gt;
using namespace std;
int main()
{
    const int k=20;
    const int *p = &amp;amp;k;
    cout&amp;lt;&amp;lt;*p;
    p = NULL; // can be changed
    p = p+1; // can be changed
    //*p =100; // Error assignment of read-only location '* p'
     //This error comes as pointer is declared as pointer to const
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Constant pointer to variable&lt;/strong&gt; : Value pointed by constant pointer can be changed but pointer can not be changed. This type of pointer can hold same address for life time and it can not be changed to point to other variable. Constant pointers needs to be initialized where it is declared.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Constant Pointer can be declared by placing const keyword after * and before pointer name.&lt;br&gt;
Example.&lt;br&gt;
int a =10 ;&lt;br&gt;
int * &lt;strong&gt;const&lt;/strong&gt; p = &amp;amp;a;&lt;/p&gt;

&lt;p&gt;Below code snippet demonstrates restriction on changing pointer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include&amp;lt;iostream&amp;gt;
using namespace std;
int main()
{
    int k=20;
    int * const p = &amp;amp;k;
    cout&amp;lt;&amp;lt;*p&amp;lt;&amp;lt;endl;
    //p = NULL;// can  not be changed
    //p = p+1; // can  not be changed
    *p =100; // value ( *p) can be changed   
    cout&amp;lt;&amp;lt;*p&amp;lt;&amp;lt;endl;
    return 0;
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Array name is internally a constant pointer to first element of array and it stores base address of array. Function name is also internally constant pointer.&lt;/p&gt;

&lt;p&gt;Below snippet demonstrates array and function names restriction.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include&amp;lt;iostream&amp;gt;
using namespace std;
int add(int a, int b)
{  
    return a+b; 
}

int main()
{
    char name[50]= "Priyanka";
    //name++; //error: lvalue required as increment operand
    //add = add+1; //error: assignment of function 'int add(int, int)'
    cout&amp;lt;&amp;lt;name;
    return 0;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Constant pointer to constant&lt;/strong&gt; : Value pointed by constant pointer to constant can not be changed also pointer can not be  changed. Here pointer and value both are read only. Its used to pass or insert &lt;strong&gt;this&lt;/strong&gt; pointer in member functions of class.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Constant Pointer to constant can be declared by placing const keyword before datatype as well as  const after * and  before pointer name.&lt;br&gt;
Example.&lt;br&gt;
const int a =10 ;&lt;br&gt;
&lt;strong&gt;const&lt;/strong&gt; int * &lt;strong&gt;const&lt;/strong&gt; p = &amp;amp;a&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include&amp;lt;iostream&amp;gt;
using namespace std;
int main()
{
    const int k=20;
    const int *const p = &amp;amp;k;
    cout&amp;lt;&amp;lt;*p&amp;lt;&amp;lt;endl;
    p = NULL; // Can not be changed error: assignment of read-only variable 'p'
    p = p+1; // Can not be changed error: assignment of read-only variable 'p'
    *p =100; // Can not be changed error: assignment of read-only location '*(const int*)p'
    cout&amp;lt;&amp;lt;*p&amp;lt;&amp;lt;endl;
    return 0;
} 


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt; :  Pointers are flexible and they support fastest data operations by directly working on addresses. While working with pointer if developers make use of const keyword they control write access to data.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>cpp</category>
      <category>pointer</category>
    </item>
  </channel>
</rss>
