Pointer and Array in C

Pointer and Array in C

Pointer and Array in C are closely related in C. In fact, an array declared as
int A[10];

 It can be accessed using its pointer representation. The array name  A is a constant pointer to the first element of the array. So A can be considered a const int*. Since A is a constant pointer, A = NULL will be an illegal statement. Arrays and pointers are the same in terms of how they are used to access memory. And, the important difference between them is that a pointer variable can take different addresses as values whereas, in the case of the array it is fixed.

 

Diagrammatic Representation

 Pointer and Array in C

 

From the above example, it is clear that &x[0] is equivalent to x. And, x[0] is equivalent to *.

Similarly,

  • Firstly &x[1] is equivalent to x+1 and x[1] is equivalent to *(x+1).
  • Then &x[2] is equivalent to x+2 and x[2] is equivalent to *(x+2). 
  • &x[3] is equivalent to x+3 and x[3] is equivalent to *(x+3).
  • &x[i] is equivalent to x+i and x[i] is equivalent to *(x+i).

 

Program

#include <stdio.h>
int main(){
 int i,class[6],sum=0;
 printf("Enter 6 numbers:\n");
 for(i=0;i<6;++i){
 scanf("%d",(class+i)); // (class+i) is equivalent to &class[i]
 sum += *(class+i); // *(class+i) is equivalent to class[i]
 }
 printf("Sum=%d",sum);
 return 0;
}

 

Output

Enter 6 numbers:
2
3
4
5
3
4
Sum=21

Pointers and 2D Array

Create pointer for the two-dimensional array

Our pointer will also be of type int because we have created the two-dimensional integer array num.

We assign the address of the first element of the array num to the pointer ptr using the address of & operator.

int *ptr = &num[0][0];

 

Accessing the elements of the two-dimensional array via a pointer

The two-dimensional array num will save as a continuous block in the memory. So, if we increment the value of ptr by 1 we will move to the next block in the allocated memory.

 

Accessing the value of the two-dimensional array via pointer using row and column of the array

If we want to get the value at any given row, a column of the array then we can use the value at the address of the * operator and the following formula.

arr[m][n] = *(ptr + (m x no_of_colmns + n))

Here, arr is a two-dimensional array, and m and n denote the mth row and nth column of the array.

ptr holds the address of the first element of the array

no_of_colmns denotes the total number of columns in the row of the array.

 

Program

#include <stdio.h>

int main(void) {
  
  // 2d array
  int num[3][4] = {
    {1, 2,  3,  4},
    {5, 6,  7,  8},
    {9, 10, 11, 12}
  };
  
  int
    ROWS = 3,
    COLS = 4,
    i, j;

  // pointer
  int *ptr = &num[0][0];
  
  // print the element of the array via pointer ptr
  for (i = 0; i < ROWS; i++) {
    for (j = 0; j < COLS; j++) {
      printf("%d ", *(ptr + i * COLS + j));
    }
    printf("\n");
  }
  
  return 0;
}

 

Output

1 2 3 4
5 6 7 8
9 10 11 12

 

Related Posts

Function Pointer in C

Structure Pointer in C

Double Pointer in C

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *