Relation Between Array and Pointer in C

This tutorial will tell you all about the Array and Pointer concept that you should know about it while doing programming in C and C++. 

You will know what is an array and how to implement it with the help of a pointer what we call it the relationship between array and the pointer.


Relation Between Array and Pointer in C

Before knowing the relationship between an array and a pointer, we should know what an array and pointer is.

Let's understand the basic concept of array and pointer first then we will learn how to implement an array with the help of a pointer in C program.

Array is a derived data type. Array is a collection of homogeneous types of data. We can say that an array is a kind of variable that can hold more than one value at the same time.

Generally, a simple variable can hold a single value at a time but the array can hold many. It depends upon the size of the array.

In order to hold many values, an array is allotted with many blocks having index numbers to access them. These index numbers always start with 0 and end with (the size of the array-1).

Each array needs to be declared first before being used in a program. In declaration, a subscript is associated with the name of the array containing an integer number.

This integer number determines the size of the array and array is always followed by the for loop.


Syntax of array:

dataTypes NameofArray[SizeofArray];
for instance,
int rollno[15];
Here, int is data type and rollno is a variable of an array type (name of an array) and 15 is the size of the array (means rollno can hold 15 integer value under rollno container).

The index of rollno array starts with 0 and ends with 14. Remember that index is starting with 0, it means that rollno[0] will indicate the first value stored in rollno and rollno[14] will indicate the last value stored inside rollno.

We can access any index value by writing its index number in the big bracket.

Suppose, we want to access the value stored at 13 index of rollno. We can access it by writing rollno[13].

Example: array in c

#include<stdio.h>
#include<conio.h>
void main() 
 {   
  int a, arrayB[8];   
  clrscr();  
 
  printf("Enter 8 values: \n");  
 
  for(a=0; a<8; a++)   
   {   
    scanf("%d", &arrayB[a]);   
   }   

  printf("Printing the array: \n");

  for(a=0; a<8; a++)   
   {
    printf("%d", arrayB[a]);  
   }

  getch();
 }

Output



Enter 8 values:
10
-5
2
100
58
23
-90
51
Printing the array:
10
-5
2
100
58
23
-90
51


Explanation:


10 is stored at arrayB[0] 
-5 is stored at arrayB[1]
2 is stored at arrayB[2]
100 is stored at arrayB[3]
58 is stored at arrayB[4]
23 is stored at arrayB[5]
-90 is stored at arrayB[6]
51 is stored at arrayB[7]


Types of Array

Array is of two types. They are as follows
  1. Single Dimension Array
  2. Multi Dimension Array

Single Dimension Array

Single dimension array is that array that has only one subscript in the notation of array. It is also called a 1-dimensional array or 1-d array.



for example,
int students[25];
Here, num is a single array or 1-dimensional array or 1-d array that can hold a similar integer value only and can hold about 25 value at the same time. 

25 denotes the size of the array students. The index of array num starts with 0 and ends with (size of array -1) which means the index of array num ends with 24.
Following is the example of a single-dimensional array.


Example: 1-d array

   #include<stdio.h>
   #include<conio.h>
   void main() 
    {   
     int i, num[10];   
     clrscr();  
 
     printf("Enter 10 values: \n");   

     for(i=1; i<=10; i++)   
      {   
       scanf("%d", &num[i]);   
      }   

     printf("Printing the array: \n");

     for(i=1; a<=10; i++)   
      {
       printf("%d", num[i]);   
      }   

     getch();   
    }

Output


Enter 10 values:
20
-15
22
10
5
234
-9
50
11
9
Printing the array:
20
-15
22
10
5
234
-9
50
9
11

Explanation:

20 is stored at num[0] 
-15 is stored at num[1]
22 is stored at num[2]
10 is stored at num[3]
5 is stored at num[4]
234 is stored at num[5]
-9 is stored at num[6]
50 is stored at num[7]
9 is stored at num[8]
11 is stored at num[9]


Multi Dimension Array

Multi dimension array is further classified according to the dimension of the array like a 2-dimensional array, 3-dimensional array, 4-dimensional array and so on.

2-Dimensional Array: A 2-dimensional array has 2 subscript namely the number of rows and number of columns. We can view a 2-d array in the form of a table.

The programs of a matrix is the best example of a 2-d array because the matrix is the combination of rows and columns.

for example,

int newvar[2][3];
Here, newvar behaves like a 2-dimensional array where 2 denotes the number of rows and 3 denotes the number of columns.

The row index starts with 0 and ends with (size of row-1) and the column index also starts with 0 and ends with (size of column-1).

So, newvar array can be seen as a table of 2 rows and 3 columns. Assuming a 2-dimensional array as a table makes us easier to understand it.

All possible combination of index with rows and columns are as below

newvar[0][0] denotes data stored in first row and first column block.
newvar[0][1] denotes data stored in first row and second column block.
newvar[0][2] denotes data stored in first row and third column block.
newvar[1][0] denotes data stored in second row and first column block.
newvar[1][1] denotes data stored in second row and second column block.
newvar[1][2] denotes data stored in second row and third column block.


3-dimensional array: A 3-dimensional array has 3 subscript. In this case, the first subscript represents the number of tables.

The second subscript represents the number of rows each table having and the third subscript represents the number of columns each table having.

for example,


int powerful[2][2][3];
Here, 2 represents that there are 2 tables. 3 represents the number of rows and 4 represents the number of columns each table has.

Explanation:


powerful[0][0][0] denotes data of first row and first column of first table.
powerful[0][0][1] denotes data of first row and second column of first table.
powerful[0][0][2] denotes data of first row and third column of first table.
powerful[0][1][0] denotes data of second row and first column of first table.
powerful[0][1][1] denotes data of second row and second column of first table.
powerful[0][1][2] denotes data of second row and third column of first table.
powerful[1][0][0] denotes data of first row and first column of second table.
powerful[1][0][1] denotes data of first row and second column of second table.
powerful[1][0][2] denotes data of first row and third column of second table.
powerful[1][1][0] denotes data of second row and first column of second table.
powerful[1][1][1] denotes data of second row and second column of second table.
powerful[1][1][2] denotes data of second row and third column of second table.

Pointers in C

Pointers are the special variables that can hold the address of a variable. Each and every variable is given some address of memory for storing data at that memory address.

Every variable of a program finds a memory space having some unique address. If we want to keep the address in some variable as a value then that variable must be a pointer variable.


Declaration of Pointer Variable in C

Asterisk (*) is used to declare a pointer variable in C and is always prefixed with the name of the variable in the declaration part of a function.

int num = 25, *point;
*point = &num;
Here, num is a variable of integer type and it is holding 25 as a value and point is a pointer that is now ready to hold the address of another variable. In this case, the pointer point is holding the address of the integer num.

Pointer operation can be done with the help of two operators which are * (asterisk) and & (ampersand).

Asterisk (*) is used for the declaration of the pointer and ampersand (&) is used to give the address of the variable to the pointer.



*pointerName;   // to declare pointer
*(address)      //to access the value at any given address
Asterisk (*) operator is used for doing the following two things.

  1. Declaration of pointer
  2. Accessing the value

Declaration:


Asterisk (*) is used to declare a pointer variable in C and is always prefixed with the name of the variable in the declaration part of a function.

Accessing the value:

Asterisk (*) is used to access the value at any given address. Since a pointer variable can have an address of the value (as a value), this operator is always used with this variable only.

int num = 7, *p;      // let say 1pqg is the address of num where 7 is stored.
p = &num;
*(1pqg)      // give 7 as output.
*(&p)       // give 7 as output.
*(p)       // give 7 as output.


Ampersand (&) operator is used to access the address of any variable.


Example: pointer program in c


   #include<stdio.h>
   #include<conio.h>
   void main() 
    {   
     int num1 = 7, num2 = 8, *i, *j;

     i = &num1; 
     j = &num2; 

     *i = *i+*j; 
     *j = *i+*j;

     clrscr();  

     printf("%d \n", num1);
     printf("%d \n", num2);

     getch();   
    }

Output


15
23

Relation Between Array and Pointer in C 

Handling Array with the help of Pointer


#include<stdio.h>
   #include<conio.h>
   int main() 
    {   
     int n[5], *p, i;
     p = n[0];        // or p =n; 

     printf("Enter five values \n");
   
     for (i=0; i<5; i++)
      {
       scanf("%d", p+i);
      }

     printf("Value of Array is: \n");
   
     for (i=0; i<5; i++)
      {
       printf("%d \n", *(p+i));
      }

     return 0;
     getch();   
    }

Output


Enter five values
42
-12
56
23
10

Value of Array is:
42
-12
56
23
10

<< Previous                                                                                         Next >>
Previous
Next Post »

Comment Below: ConversionConversion EmoticonEmoticon