Skip to main content

Basic Concept of Pointer in C and it's uses #DOTC_Mdb

Pointers in C.

 

Hello Everyone,

Today we will discuss about Pointer in C program. In Simple word Pointer in C is special type of variables which holds memory address of any variable. Before going to know more about Pointer . Please see my below blog where in archive folder where you will able to find all the previous topics.

dheeraj60.blogspot.com

Let’s Start with Pointer in C . Pointer is basically used to see the address of variable which we assigned in the program. In other word we can say A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address. Suppose we declare any variable of int type like int i=20; and I want to see the address of that value then through pointer we can easily see the address of variable i which stored in the memory location with different format.

Syntax of Declaring Pointer is :

Int *p;  -- for integer data type

double *p; - for double data type

float *p ; - For float data type

char * Ch – for character data type

Note : Actual data type of the value of all pointers, whether it is integer, float, double or character is saved in  a long hexadecimal number that represents a memory address of variables.

 

Let’s understand with one simple Program where we use integer data type and assigned that value and get the address by using Pointer.

 

Program is as below:

#include <stdio.h>

#include <stdlib.h>

int main()

{

 int a = 100;

   int *p;

   p=&a;

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

   printf("Address of variable a is %p", *p);

   return 0;

   }

Here  in above program we declare pointer int *p; and already assigned the value of variable a=100  and address of variable assigned in p and through print f command we display the variable value with  its address also by using pointer by using %p in printf as we use %d for int data type.

Below is the output of above code in Compiler Code Block with both variable values with its address location.

 Here we see both the variable value with address in program by using Pointer.

 

Let’s write one more Program where we can see the  values of  variables  with address by using pointer with same concept.

 

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

 int  a = 100;  

   int  *p;       

 

   p = &a; 

 

   printf("Address of var variable: %x\n", &a  );

 

      printf("Address stored in p variable: %x\n", p );

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

 

 

   }

 

Here actual variable declaration of a is 100 then we declare pointer *p  then we store the address of a in pointer variable by using  p = &a . in second print f statement we print address stored in pointer variable and in last print f we access the value using the pointer.

 

Below is the output of above Program in compiler  code  Block .

 


Here you can easily trace the address of variable and can access the actual value using the pointer. Hope you are able to understand the basics of Pointer.

 

Let’s understand one more program by using Array in pointer to see the output of value with address.

 

Program is Below

 

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

 int  marks[] = {70,80,90};

   int  *p[3],i;

 

for (i=0; i<3; i++)

{

    printf("%d\n",marks[i]);

    p[i]=&marks[i];

}

 

for (i=0; i<3; i++)

{

  printf("%d",*p[i]);

}

return 0;

   }

 

Here through array we can display the values of marks and through pointer we also show the address of variable.

Please see the output of above program in compiler code block for your understanding.

 

 

See the output as we can see the marks and address of variable which we used to see by pointer using array.

Hope you are able to understand the basic concept of Pointer and can write programs to get the address of any variables etc by using simple as well as array program so please practice as much as you can.Pointer is a big topic which includes Pointers to pointers and pointers used in functions etc which I am going to cover in next blog.

 

Please read this blog carefully so that it’s easy for you to understand more topics in Pointers with Programs and it’s uses and benefits.

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

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

Aggregate Functions and Group By Clause in Oracle with Examples #Diksha Oracle Training Centre

  Aggregate Functions and Group By Clause Oracle with Examples.   Hello Everyone, Today I will   discuss about   Aggregate function in SQL by using Group By Clause and different clauses with some examples . Please go tjrough my previous blogs in Archive folder for classification of SQL, Commands and   SQL joins for your understanding.   Aggregate Functions Allows us to perform a calculation on a set of values to return a single value . We can use Group by Clause to group the result-set by one or more columns. Also we can use Having clause to restrict or filter the data as per our requirement. Note: Whenever we use Aggregate function in SQL we can’t able to use where condition. To restrict or filter the record we need to use having clause instead of Where. Below is the most commonly used Aggregate function in SQL.   MAX : Max function   used to get the maximum values in a set of values. COUNT : This function used to count rows in ...