Accessing Array Elements using Pointers in C
Accessing Array Elements using Pointers in C Program Description:
Write a Program to Access the array elements using pointers in c programming language. The program should accept an array and print array elements using pointers. To access the array’s elements, we will use standard pointer notation (by dereferencing the pointer) as well as pointer subscript notation.
Prerequisites:
It is recommended to know the basics of the C Pointers and C Arrays, So please go through the following tutorials learn more about these concepts.
- Pointers in C Programming Language – Declare, Initialize, and use pointers
- Pointer Arithmetic in C Language
- Pointer to Pointer or Double Pointer in C Language
- Arrays in C with Example Programs
- Functions in C – How to Create and Use functions in C
Accessing Array Elements using Pointer in C Program:
Here is the program to access the array elements using pointers in c language. We have accessed the integer array heights elements using a pointer variable ptr.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
/* Program to Access array elements using Pointers in C sillycodes.com */ #include <stdio.h> // Size of Array #define SIZE 5 int main() { // create an array int heights[SIZE] = {150, 195, 172, 180, 165}; // create a pointer and store address of heights int *ptr = heights; printf("Get Array Elements using Pointer\n"); // use pointer to get values of array for(int i=0; i<SIZE; i++) { // use *(ptr+i) to get the values of array elements printf("Value of heights[%d]=%d \n", i, *(ptr+i)); } return 0; } |
As we can see from the above program, We started the program by creating an integer array named heights, Then we stored the address of the heights array in the integer pointer ptr.
int *ptr = heights;
Note that, We used the heights array. As the array name also holds the memory address of the first element of the array.
Then we used a for loop to iterate over the array elements and printed the elements using the pointer ptr with Index(i) (i.e *(ptr+i))
Program Output:
Let’s compile and run the program using your compiler.
1 2 3 4 5 6 7 8 9 |
$ gcc arr-ptr.c $ ./a.out Get Array Elements using Pointer Value of heights[0]=150 Value of heights[1]=195 Value of heights[2]=172 Value of heights[3]=180 Value of heights[4]=165 $ |
The program properly printed the array elements on the console.
Get Array Elements using the Pointer subscript notation [ ] in C:
Similarly, We can also use the pointer variable with the subscript notation [ ] to get the array elements.
Here is the program for getting the array elements using the pointer subscript notation in c programming language.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
/* Program to Access array elements using Pointers subscript notation in C sillycodes.com */ #include <stdio.h> // Size of Array #define SIZE 5 int main() { // create an array int heights[SIZE] = {150, 195, 172, 180, 165}; // create a pointer and store address of heights int *ptr = heights; printf("Get Array Elements using Pointer Subscript Notation\n"); // use pointer to get values of array for(int i=0; i<SIZE; i++) { // We can also use subscript notation ptr[i] printf("Value of ages[%d]=%d \n", i, ptr[i]); } return 0; } |
Note that, We used the pointer subscript notation ptr[i] to get the array element at the index i.
Program Output:
Compile the program.
$ gcc arr-ptr-subscript.c
Run the program and observe the output.
1 2 3 4 5 6 7 8 |
$ ./a.out Get Array Elements using Pointer Subscript Notation Value of ages[0]=150 Value of ages[1]=195 Value of ages[2]=172 Value of ages[3]=180 Value of ages[4]=165 $ |
As we can see from the above output, we are able to get the array elements values using the subscript notation(ptr[i])
Related Programs:
- C Tutorials Index
- C Programs Index – 300+ Programs
- Program to Demonstrate Address of Operator (’&’) in C Language
- Program to Understand the Dereference or Indirection Operator in C Language
- Size of Pointer variables in C Language
- Program to Add Two Numbers using Pointers in C
- Program to perform Arithmetic Operations using Pointers in C
- Swap Two Numbers using Pointers in C Language
2 Responses
[…] Accessing Array Elements using Pointers in C […]
[…] Accessing Array Elements using Pointers in C […]