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...

Dynamic SQL in Oracle PL/SQL #DOTC_Mdb

                                    Dynamic SQL in Oracle PL/SQL Hello Everyone, Today we will discuss about Dynamic SQL in Oracle PL/SQL and its benefits. Before reading this blog I will suggest you to see my previous blogs related with SQL and PL/SQL blog for better understanding as some of PL/SQL feature I will use in this blog. Let’s start to understand Dynamic SQL in Oracle. Dynamic SQL in Oracle PL/SQL used for generating and running SQL statements at run time. Through Dynamic SQL we can use DDL statements in PL/SQL block.   Dynamic SQL is useful when writing general-purpose and flexible programs like ad hoc query systems, when writing programs that must run database definition language (DDL) statements. We can write Dynamic SQL in two ways. 1. Native Dynamic SQL 2. DBMS_SQL 1. Native dynamic SQL in PL/SQL language enables for building and running dynamic SQL statements at ru...

Concept of Software Development life Cycle in IT industry #DOTC_Mdb

Concept of Software Development life Cycle in IT industry.   Hello Everyone, In my previous blog we discussed about Agile Model which is also a part of SDLC, but today we discussed about Software Development life cycle in details and how being software products developed or services provided through SDLC. I suggest you to please go through my previous blog that is related to Agile model also a part of SDLC which permits a company to develop software in small, quick segments. https://dheeraj60.blogspot.com/2020/07/overview-of-agile-modelmethodology.html Let’s discuss about SDLC as its play very important role in developing software products as well as providing services to the clients in simple word we can say SDLC(Software Development life Cycle ) is a process that produces software with the highest quality and lowest cost in the shortest time. SDLC includes a detailed plan for how to develop, alter, maintain, and replace a software system .SDLC is also known as Software devel...