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

Pseudo Column in Oracle #Diksha Oracle Training Centre

  Pseudo Column in Oracle. Hello Everyone, Today I will discuss about Pseudo column in Oracle. This topic is very important so before going to read this blog, I suggest you to please see my previous blogs which is in archive folder for your reference so that it’s easy for you to understand this topic. Let’s discuss about Pseudo Column in Oracle. Pseudo column: A pseudo-column is dynamic in Oracle as it behaves like a table column but is not stored in the table. Pseudo column behaves like a table column, but is not actually stored in the table. You can select from pseudo columns, but you cannot insert, update, or delete their values. A pseudo column is also similar to a function without arguments. In   Simple word we can say Pseudo column is a column that yields a value when selected, but which is not an actual column of the table. An example is RowID or SysDate. It can be use in combination with the DUAL table. Below are the Pseudo Column commonly used in Oracle Dat...