Skip to main content

Pointers and arrays in c

POINTERS AND ARRAYS:
What is an array?
An array is a data structure consisting of elements of similar data type.  The elements of an array are stored in consecutive memory locations.

How to declare an array?
Below is the template for array declaration.
datatype  name[number of elements];
          int   num[10];
Here, num is array of 10 integers.

How to access array elements?
Array indices are used to access array elements.  And the array index starts from zero.
      int arr[5] = {1, 2, 3, 4, 5};
arr[0] gives us the element at the 0th index in the array arr.  Let us try to read the array elements one by one.
arr[0] is 1 
arr[1] is 2 
arr[2] is 3 
arr[3] is 4 
arr[4] is 5 

How to access array elements using pointers?
To access array elements, assign the base address of array to the pointer.
     int *ptr;
     int arr[5] = {10, 20, 30, 40, 50};
     ptr = &arr[0];
Now, the pointer refers to the address of first element in the array arr.

*(ptr + 0) refers to the content of arr[0]

Then, *(ptr + i) refers to the content of arr[i].  So, the element at the array index i can be access by dereferencing the pointer (ptr + i).
ie. *(ptr + i) <=> arr[i]

How to write values in an array using pointers?
To write values in an array using pointers, assign the base address of array to pointer.  Then, assing value to the object refereced by pointer as shown below.
*(ptr + 0) = 100;
*(ptr + 1) = 200;
*(ptr + 2) = 300;
*(ptr + 3) = 400;
*(ptr + 4) = 500;

So, we have updated the array elements using pointers and the element in the array arr are as follows.
     arr[5] = {100, 200, 300, 400, 500};

Below program explains how to read values from an array and write values to array using pointers.


  #include <stdio.h>
  int main() {
        int i, arr[5], input;
        printf("Enter array inputs:\n");
        for (i = 0; i < 5; i++) {
                printf("arr[%d]: ", i);
                scanf("%d", &input);
                /* assigning values to array elements using pointers */
                *(arr + i) = input;
        }

        printf("\nReading array elements using pointer:\n");
        for (i = 0; i < 5; i++) {
                printf("arr[%d]: %d\n", i, *(arr + i));
        }
        return 0;
  }




  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter array inputs:
  arr[0]: 10
  arr[1]: 20
  arr[2]: 30
  arr[3]: 40
  arr[4]: 50

  Reading array elements using pointer:
  arr[0]: 10
  arr[1]: 20
  arr[2]: 30
  arr[3]: 40
  arr[4]: 50



How to access two dimensional array using pointers?
Consider the following two dimensional array.
int num[2][2] = {{1, 2}, {3, 4}};

Pointer notation           Subscript notation            Value
**num                             num[0][0]                          1
*(*num + 1)                     num[0][1]                           2
*(*(num + 1))                   num[1][0]                          3
*(*(num + 1) + 1)              num[1][1]                          4

Using the above pointer notation, we can access two dimensional array using pointers.

How to write values into a two dimensional array using pointers?
**num                   = 10;
*(*num + 1)           = 20;
*(*(num + 1))         = 30;
*(*(num + 1) + 1)   = 40;

So, we have updated the values of a two dimensional array.  And the elements in the array num are as follows.
     num[2][2] = {{10, 20}, {30, 40}};

Below program explains how to read values from a multidimensional array and write values to a multidimensional array.


  #include <stdio.h>
  int main() {
        int i, j, num[2][2] = {{1, 2}, {3, 4}};
        printf("Reading two dimensional array using pointer:\n");
        for (i = 0; i < 2; i++) {
                for (j = 0; j < 2; j++) {
                        printf("num[%d][%d]: %d\n", i, j, *(*(num + i) + j));
                }
        }

        /* writing values to a 2d array */
        for (i = 0; i < 2; i++) {
                for (j = 0; j < 2; j++) {
                        *(*(num + i) + j) = num[i][j] + 100;
                }
        }

        printf("\nReading the updated values in 2d array using pointer:\n");
        for (i = 0; i < 2; i++) {
                for (j = 0; j < 2; j++) {
                        printf("num[%d][%d]: %d\n", i, j, *(*(num + i) + j));
                }
        }

        return 0;
  }




  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Reading two dimensional array using pointer:
  num[0][0]: 1
  num[0][1]: 2
  num[1][0]: 3
  num[1][1]: 4

  Reading the updated values in 2d array using pointer:
  num[0][0]: 101
  num[0][1]: 102
  num[1][0]: 103
  num[1][1]: 104


Comments

Popular posts from this blog

Array of pointer to structure

Array of pointer to structure is nothing but an array whose elements are pointers to structure How to declare array of pointer to structure? Below is an example declaration for array of pointer to structure. struct student *arr[10]; Here, arr is an array 10 of pointer to structure student. Dynamic memory allocation: Below is the procedure to perform dynamic memory allocation for the structure pointers in an array. struct student { int age; char name[32]; }; fun() { int i = 0; struct student *arr[10]; for (i = 0; i < 10; i++) { /* dynamic memory allocation for 5 structure objects */ arr[i] = (struct student *)malloc(sizeof(struct student) * 5); } } Write a c program to pass an array of structure pointers to another function   #include <stdio.h>   #include <stdlib.h>   #include <string.h>   struct student {         int age, rollno;         char name[32];   };   /* printing details of stude...

Nested function in C

What is nested function? If a function is defined inside another function, then it is called as nested function.  Nested function example in C: Consider the following example, int add(int a, int b) {        void print(int res) {          printf("Result is %d, res);       }       print(a+b);       return 0; } Here, print() is a nested function.  Because, it is defined inside another function named add() . Example C program using nested functions: #include <stdio.h> void add(int a, int b) { void print(int res) { // nested function printf("Result is %d\n", res); return; } print(a + b); // passing sum of a and b as parameter return; } int main() { add(10, 20); return 0; }   Output:   jp@jp-VirtualBox:~/$ ./a.out   Result is 30 Previous Next

Difference between asterisk and ampersand operators in c

Address Operator (& - Ampersand): Returns address of the given variable.  Consider the following example, int *ptr, var = 10; ptr = &var; ptr = &var sets the address of the variable var to pointer ptr.  & is also called as reference operator. Let us try to understand the purpose of reference operator using the following example program.   #include <stdio.h>   int main() {         int var = 10, *ptr;         /* assigning address of variable var to pointer ptr */         ptr = &var;         /* printing the address of the variable var */         printf("Address of var is 0x%x\n", &var);         /* printing the value of pointer ptr */         printf("Value of ptr is 0x%x\n", ptr);         return 0;   }   Output:   jp@jp-VirtualBox:~/$ ./a.out   Address of var is 0x...