Skip to main content

Pointer and Array in C Program #DOTC_Mdb

Pointer and Array in C Program.

 

Hello Everyone,

Today we will discuss about Pointer and Array in C Program. I recommend you to refer my below blogs before starting this topic.

https://dheeraj60.blogspot.com/2020/06/array-and-its-beneifts-in-c-program.html

https://dheeraj60.blogspot.com/2020/06/multi-dimensional-array2d-and-3d-in-c.html

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

https://dheeraj60.blogspot.com/2020/06/pointer-to-pointer-in-c-program-dotcmdb.html

 

As we already know An array is a block of sequential data and Pointer used to get the variable address. So understand the below Program where we print the address of Array elements by using pointer.

 

#include<stdio.h>

#include<stdlib.h>

 

int main( )

{

   int *p;

    int print[4] = { 10, 11, 12, 13} ;

    p = &print[0];

   for ( int i = 0 ; i<4 ; i++ )

   {

      printf("print[%d]: value is %d and address is %p\n", i, *p, p);

      p++;

   }

   return 0;

}

 

Here we use Pointer variable as int *p and declaring the array print[4] with integer numbers , after that Assigning the address of print[0] to the pointer as  p = &print[0]  because array name represents the address of the first element and finally Incrementing the pointer so that it points to next element by using p++ on every increment.

Note: Array always starts with 0 so here 1st value will treat from 0 only There is a difference of 4 bytes between each element because that’s the size of an integer. This means all the elements are stored in consecutive contiguous memory locations in the memory.

Below is the output of above Program in Compiler Code  Block with both values and address.

 

 

You can see the output of value with address in Hexadecimal Format.

 

Let’s understand with another Program by using Pointer and Array.

#include<stdio.h>

#include<stdlib.h>

 

int main() {

  int i[4] = {1, 2, 3, 4};

  int* p;

  p = &i[2];

  printf("value for pointer *p for position 3 = %d \n", *p);

  printf(" value for pointer *(p+1) for position 4 = %d \n", *(p+1));

  printf(" value for pointer *(p-1) for position 1 = %d", *(p-1));

 

  return 0;

}

Here pointer p is assigned the address of the third element as I used p = &i[2]; and we know array starts with 0 .  Here we use pointer to display the value as per position by using increment and decrement in printf for value.

Below is the output of above program in compiler Code block for your understanding.

Here you can see the value element is displayed by pointer by using array for elements.

 

Hope you are able to understand how to use Pointer in Array Program. Please read this blog carefully and try to write more programs by using Pointers in Array and let me know if you have any doubts. Play with Pointer and Array as much as you can. In next blog I will come up with next topic.

 

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

Basic SQL Commands in Oracle Database #Diksha Oracle Training Centre

  Basic SQL Commands in Oracle Database.   Hello Everyone, In my previous blog I discussed about SQL and Classification of SQL . Today I will discuss about SQL basic commands which widely used in RDBMS. The Topics of SQL command which I am going to cover in this blog are mainly divided into four Categories: ·         DDL:   DDL consists of commands which are used to define and design the database. ·         DML:   DML consists of commands which are mainly used to manipulate the data in the database. ·        DCL: DCL Consists of commands which deal with the user permissions/access and controls in the database. ·       TCL:   TCL Consist of commands which deal with the transaction in the database. If you want to explore theory part please follow my below blog as in today blog I will discuss about how to use the commands. h...

PL/SQL in Oracle and basic Programs in PL/SQL #Diksha Oracle Training Centre

              PL/SQL in Oracle with basic Programs. Hello Everyone, Today I will discuss about PL/SQL in Oracle .In Previous Blog we discussed about cursor and anonymous block used in cursor as well as uses of %TYPE and %ROWTYPE attribute. Before starting this topic please go through my below blog. https://dikshaoracletraining.blogspot.com/2020/10/cursor-and-types-of-cursor-in-oracle.html https://dikshaoracletraining.blogspot.com/2020/11/benefits-of-type-and-rowtype-in-oracle.html PL/SQL is basically a Procedural Language in SQL and in PL/SQL we use separate two parts first is Anonymous Block and second is Named Block. The PL/SQL programming language was developed by Oracle Corporation in the late 1980s as procedural extension language for SQL relational database. PL/SQL high-performance transaction-processing language.PL/SQL is completely portable.   PL/SQL provides a built-in, interpreted and OS independent programming environment. ...