Skip to main content

Pointer To Pointer in C Program #DOTC_Mdb

Pointer to Pointer in C.


Hello Everyone,

In Previous blog we discussed about pointers in C. Today we will discuss about Pointer to Pointer in C. Before starting this topic please go through my previous blog as below for your reference.

https://dheeraj60.blogspot.com/2020/06/basic-concept-of-pointer-in-c-and-its.html

 

Let’s Start with Pointers to Pointer

As we know now pointer is basically used to find the address of variable. When a pointer holds the address of another pointer then such type of pointer is known as pointer-to-pointer. 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.

Syntax for Declaring Pointer to Pointer

**p;

In Pointer we use to take 1 *, but in pointer to pointer we need to take 2 * to get the address of second pointer and the location that contains the actual value.

 

Let’s take 1 simple example to understand Pointer to Pointer Program.

 

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

  int a=100;

 int *p;

 int **p1;

 p = &a;

 p1 = &p;

 

     /* Possible ways to find value of variable num*/

     printf("\n Value of num is: %d", a);

     printf("\n Value of num using pointer1 is: %p", p);

     printf("\n Value of num using pointer2 is: %p", p1);

 

return 0;

}

 

Here * p is normal pointer which we used in previous blog to get the address of variable and int **p1 is Pointer to pointer .

p = &a; we used to Assigning the address of variable a to the pointer p and p1=&p used to assigning the address of pointer p1 to the assigning the address of pointer p to the pointer to pointer.

So the output of the program we can see three other value one is variable value which we assigned and another address of variable and the last one we will see the pointer to pointer value.

Below is the output of above Program in Compiler Code Block.

 

  

Here we are able to see all the three different values of variables.

Note: Pointer to Pointer in C. We already know that a pointer points to a location in memory and thus used to store the address of variables. So, when we define a pointer to pointer. The first pointer is used to store the address of the variable and the second pointer is used to store the address of the first pointer.

 

Let’s understand one more program to see all probable values by using Pointer to Pointer.

Program is as below:

 

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

  int a=10;

int *p;

int **p1;

p=&a;

p1=&p;

printf("Address of number variable is %x \n",&a);

printf("Address of p variable is %p \n",p);

printf("Value of *p variable is %d \n",*p);

printf("Address of p1 variable is %p \n",p1);

printf("Value of **p1 variable is %d \n",**p1);

return 0;

}

 

Here we will see the actual variable value and also we can see the address of pointer and pointer to pointer with actual value as well.

 

Below is the output of above program in Compiler Code Block.


Here we can see all the probable values of variables with address and pointer to pointer value as well with actual value. Hope you are able to understand Pointers to Pointers now .

Advantages of pointers are

1.      Pointers allow management of structures which are allocated memory dynamically.

2.      Pointers allows passing of arrays and strings to functions more efficiently.

3.     Pointer makes possible to pass address of structure instead of entire structure to the functions.

Pointer is commonly used in Functions and Array . As of now i haven’t started Functions in C so in next blog i will come up with Function in C and will use Pointer later by using Functions . Pointer is commonly used in Functions and Arrays. So please go through this blog carefully and if any doubt let me know.

Functions plays very important role in C Program so in next blog i will come up with Function in C and later we can use Pointer in Function also. So please try to understand this blog as later we will use this in Functions as well.

Thanks.

 

Comments

Post a Comment

Popular posts from this blog

SQL and Classification of SQL in Oracle Database #Diksha Oracle Training Centre

  SQL and Classification of SQL in Oracle Database.   SQL is Structured Query Language , which is used for storing, manipulating and retrieving data stored in a relational database .SQL is the standard language for RDBMS. All the Relational Database Management Systems (RDMS) like Oracle, MySQL, Sybase, Informix, IBM DB2 and Microsoft SQL Server use SQL as their standard database language. Oracle is one of the more secured database as compared to other databases. Importance of   SQL : SQL and PL/SQL is a backend process where all data is stored and retrieved in GUI which created either by any programming languages like Java, C++, PHP etc. so we need to have very secure database so that there will be no impact for users. SQL allows users to access data in the relational database management systems. SQL is used to communicate with a database.SQL and PL/SQL allows users to create and drop databases tables , views , stored procedures , functions , packages , trigger et...

Materialized View uses and Benefits in Database #DOTC_Mdb

Materialized View uses and Benefits in Database. Hello Everyone, Today we will discuss about Materialized view as it’s play important role in database. We already discussed about Simple Views and complex views in my previous blog. Before Materialized view go through my previous blog which related to simple view. https://dheeraj60.blogspot.com/2020/05/benefits-of-creating-and-using-view-in.html As we know View is not a database object and not like table which is stored in database, but view can be created from base table a view is an SQL statement that’s stored in the database. This statement, or view, has a name.A view looks and acts a lot like a table. It has columns and rows, and can be included in SELECT queries just like a table. In other word we can say View is a virtual/logical table which is basically used for security purpose. Let’s understand   about   Materialized view : A materialized view is a view that stores the results of the view’s query. Whenever you que...

Top 50 Interview Questions On SQL and PL/SQL #DOTC_Mdb

                    Top 50 Interview Questions On SQL and PL/SQL. Today we will Discuss Top 50 interview questions and answers of SQL and PL/SQL which is frequently asked in interview.     Question 1: What is SQL and Classification of SQL? Answer SQL is a Structure Query Language which is vastly used in RDBMS database like Oracle, Sybase, DB2 , Microsoft SQL server etc.   Classification of SQL is as below: DDL (Data Definition Language):  Commands are  create , alter , drop , truncate etc DML (Data Manipulation Language) : Commands are  insert,update and delete . TCL (Transaction Control Language ) : Commands are  Commit , Rollback and Save point. DCL (Data Control Language) : Commands are Grant , Revoke Question 2:    What is meant by Joins? What are the types of join? Answer Joins are basically used to extract/get data from multiple tables using s...